Monitoring JVM memory usage with MX Beans

This is a post summarizing my initial research into reliable ways to measure the heap memory usage for the JVM. Previously, I have used the linux top command and look at resident memory for a rough estimate of the heap usage for JVM process. However, I found that inaccurate and I needed more precise measures to reflect some of the memory saving techniques I employed in my optimized MapReduce runtime. Suggestions from Shams indicated that Java MXBean could be used to see the memory usage. Also, I could try to acquire a heap dump and use Visual VM or other tools to see the heap objects, as put forward by Prof. Mellor-Crummey.

To use MXBeans to monitor the JVM heap utilization, I used the following line of code

MemoryMXBean memBean = ManagementFactory.getMemoryMXBean() ;
MemoryUsage heap = memBean.getHeapMemoryUsage();
MemoryUsage nonHeap = memBean.getNonHeapMemoryUsage();
System.err.println(String.format("Heap: Init: %d, Used: %d, Committed: %d, Max: %d", heap.getInit(), heap.getUsed(), heap.getCommitted(), heap.getMax()));

It retries the heap attribute from MxBeans factory. The sub fields, init, used, committed, max represent

  • init: related to the minimum amount of heap given, the amount specified by the -Xms option
  • used: the amount used by the program, including the live objects and garbage collected objects
  • committed: amount of memory that is committed for the JVM to use
  • max: maximum amount of memory that can be used by the JVM, related to the -Xmx option.

The official documentation can be found here

http://docs.oracle.com/javase/7/docs/api/java/lang/management/MemoryMXBean.html

 

  • getHeapMemoryUsage

    MemoryUsage getHeapMemoryUsage()
    Returns the current memory usage of the heap that is used for object allocation. The heap consists of one or more memory pools. The used and committed size of the returned memory usage is the sum of those values of all heap memory pools whereas the init and max size of the returned memory usage represents the setting of the heap memory which may not be the sum of those of all heap memory pools.The amount of used memory in the returned memory usage is the amount of memory occupied by both live objects and garbage objects that have not been collected, if any.

    MBeanServer access:
    The mapped type of MemoryUsage is CompositeData with attributes as specified in MemoryUsage.

    Returns:
    MemoryUsage object representing the heap memory usage.
Advertisements
This entry was posted in Java. Bookmark the permalink.

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