DotNetSlackers: ASP.NET News for lazy Developers

Friday, January 7, 2011

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.
 Collapse
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.
 Collapse
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.

No comments:

Post a Comment