Click here to Skip to main content
15,885,216 members
Articles / All Topics

Monitor your application’s memory usage

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
6 Jan 2011CPOL2 min read 7.8K   1  
Monitor your application’s memory usage

Efficient utilization of memory is vital for any application’s performance. Even all modern development environments provide automatic memory management capability through Garbage Collection (GC) mechanism, we cannot completely rely on GC.

In several situations, we create multiple objects and most objects are within the scope of the application, but we seldom use those objects. Even worse, sometimes we open database, network connections or file streams but do not close them. These mistakes eat up valuable system memory resulting in degradation of application performance.

Even though there are multiple memory profiling tools available in the market, we can still check how our application is performing in context of memory utilization by creating our own small utility class.

Let’s try to develop a class that can be hooked to any of the applications which keep on checking our applications heap memory utilization in every 30 second interval.
To develop this utility, we need to take care of three important Java classes:

  1. java.lang.Runtime – Every Java application has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime method.
  2. java.util.TimerTask – A task that can be scheduled for one-time or repeated execution by a Timer.
  3. java.util.Timer – A facility for threads to schedule tasks for future execution in a background thread. Tasks may be scheduled for one-time execution, or for repeated execution at regular intervals.

The first step should be to create a class called MemoryInfo. Its purpose is to override run TimerTask.run() method and within that method get the memory information from Runtime object.

Java
package learn.memory.info;

import java.util.Date;
import java.util.TimerTask;

/**
* @author Bikash
*/
public class MemoryInfo extends TimerTask {
  int mMB;
  Runtime mRuntime;

  public MemoryInfo() {
    mMB = 1024 * 1024;
    mRuntime = Runtime.getRuntime();
  }

 @Override
 public void run() {
   System.out.println("##### Heap utilization statistics [MB] at "+ new Date() +" #####");
   //Print used memory
   System.out.println("Used Memory: " + 
	(mRuntime.totalMemory() - mRuntime.freeMemory()) / mMB);

   //Print free memory
   System.out.println("Free Memory: "+ mRuntime.freeMemory() / mMB);

  //Print total available memory
  System.out.println("Total Memory:" + mRuntime.totalMemory() / mMB);

  //Print Maximum available memory
  System.out.println("Max Memory:" + mRuntime.maxMemory() / mMB);
  }
}

Now, the step is to attach an instance of the MemoryInfo class with our application. For the purpose, let’s create a Main class which is responsible for scheduling this task for repeated fixed-delay execution.

Java
package learn.memory.info;

import java.util.Date;
import java.util.Timer;

/**
* @author Bikash
*/
public class Main {
  public static void main(String[] args) {
    Timer tick = new Timer("Show Memory Info");
    tick.schedule(new MemoryInfo(), new Date(), 30 * 1000);
  }
}

Now, the output should look like the below figure:

Console Output

This application may not be sufficient to fulfill the requirement of our memory profiling but it can surely give us an overview of how Java task scheduler works and how we can check our memory status.


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Société Générale
India India
Summary:

1. Extensive working experience in web and windows application development, database programming using Java and .Net technologies.

2. Good knowledge in SDLC and Processes like project planning, requirement gathering, development, test planning, release management and production support.

3. Good knowledge and working experience in following methodologies:
a. Agile Development Approach
b. Test Driven Development
c. Behavior Driven Development
d. Continuous Integration & Delivery Model.

4. Excellent communication & analytical skills, good team player, great mentoring capability.

5. Interaction with customer and team spread over different geographical area.

Technologies / Languages: J2SE 5/6, J2EE, .Net 2.0/3.5, C#, ASP.NET, AJAX, XHTML, CSS, JavaScript, jQuery, PL/SQL, Web Services (SOAP based), Winforms, Hibernate, Spring, GWT, XML, XSD, XPath, SAX, JAXB, JUnit, JBehave, Mockito, Selenium, StringTemplate, Log4J, Apache Commons API

Database: Oracle, SQL Server, MySQL, Sybase

Application Server: Tomcat, Sun Application Server, JBoss, GlassFish, Jetty, IIS

Development Environments: Eclipse, NetBeans, Visual Studio.

Designing Tools: Enterprise Architect, Microsoft Visio

Version Control: SVN, Perforce

Build Management: Hudson & JCruisemonitor, TeamCity

Bug Tracking Tools: HP Quality Center, Bugzilla, JIRA

Specialties:
1. Agile Test and Behavior Driven Development
2. Continuous Delivery Approach
2. Spring IOC and MVC
3. Web Services

Comments and Discussions

 
-- There are no messages in this forum --