Home > Java Know Hows > Monitor your application’s memory usage

Monitor your application’s memory usage

Utilization of memory efficiently is vital for any application’s performance. Even all modern development environments provide automatic memory management capability though Garbage Collection (GC) mechanism, we cannot completely rely on GC.
In several situations we create multiple objects and most the objects within the scope of the application, but we seldom use those objects. Even worse sometime we open database, network connections or file stream but do not close them. These mistakes eat up valuable system memory resulting degradation of application performance.

Even those 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 application which keeps on checking our applications heap memory utilization in every 30 second interval.
To develop this utility we need to take care 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.


First step should be 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.

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.

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 output should be 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 gives as an overview of how Java task scheduler works and how we can check our memory status.

About these ads
Categories: Java Know Hows
  1. February 4, 2011 at 10:24 pm | #1

    This is very interesting, You’re a very skilled blogger. I have joined your rss feed and look forward to seeking more of your magnificent post. Also, I’ve shared your site in my social networks!

  2. February 5, 2011 at 7:41 am | #2

    You really make it seem so easy with your presentation but I find this matter to be really something which I think I would never understand. It seems too complicated and very broad for me. I am looking forward for your next post, I will try to get the hang of it!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: