Pages Navigation Menu

Coding is much easier than you think

Getting JVM heap size, used memory, total memory using Java Runtime

Getting JVM heap size, used memory, total memory using Java Runtime

 
Java’€™s Runtime class provide lot of information about the resource details of JVM(Java Virtual Machine). The memory consumed by the JVM can be read by different methods in Runtime class.
 
In the following example, at start of program we get initial size of heap of JVM by calling freeMemory, totalMemory and maxMemory and then we create thousands of object which occupy space in heap which forces JVM to extend heap, now call to total memory, free memory will return different value based on current heap size but max memory will still return same. Suppose, when the object created requires large amount of memory, then JVM will begin to throw OutOfMemoryError.
 


import java.util.ArrayList;
public class TestMemoryUtil
{
private static final int MegaBytes = 1024 * 1024;

public static void main(String args[]) 
{

long totalMemory = Runtime.getRuntime().totalMemory() / MegaBytes;
long maxMemory = Runtime.getRuntime().maxMemory() / MegaBytes;
long freeMemory = Runtime.getRuntime().freeMemory() / MegaBytes;

System.out.println("**** Heap utilization Analysis [MB] ****");
System.out.println("JVM totalMemory also equals to initial heap size of JVM :"+ totalMemory);
System.out.println("JVM maxMemory also equals to maximum heap size of JVM: "+ maxMemory);
System.out.println("JVM freeMemory: " + freeMemory);

ArrayList<String> objects = new ArrayList<String>();

 for (int i = 0; i < 10000000; i++) 
 {
	objects.add(("" + 10 * 2710));
 }

totalMemory = Runtime.getRuntime().totalMemory() / MegaBytes;
maxMemory = Runtime.getRuntime().maxMemory() / MegaBytes;
freeMemory = Runtime.getRuntime().freeMemory() / MegaBytes;

System.out.println("Used Memory in JVM: " + (maxMemory - freeMemory));
System.out.println("totalMemory in JVM shows current size of java heap:"+totalMemory);
System.out.println("maxMemory in JVM: " + maxMemory);
System.out.println("freeMemory in JVM: " + freeMemory);

}
}

 

Output:

 

**** Heap utilization Analysis [MB] ****
JVM totalMemory also equals to initial heap size of JVM : 15
JVM maxMemory also equals to maximum heap size of JVM: 247
JVM freeMemory: 15
Used Memory in JVM: 202
totalMemory in JVM shows current size of java heap : 118
maxMemory in JVM: 247
freeMemory in JVM: 45

 

lightbulb3 For printing the memory utilization in Kilobytes, just assign the value 1024 to the int variable MegaBytes.

 

Note : This is not the best way to know the sizes and in practice it will report less size that what have you specified in €“Xmx and €“Xms but still its working solution for most of needs.

About Mohaideen Jamil


Am currently working as a Struts 2 Developer in a reputed IT Organisations. I can help you with teaching Core java and Struts 2. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

  • Srikanth Kodali

    Used Memory is always less than the values shows up on Linux top command. Why is this difference?
    top command values are not even close to TotalMemory when we give the spike. How do I get the top command values through JAVA program.