Java Heap Dump and Visual VM

This is a post summarizing my experiments with using heap dumps to analyze the objects in heap memory of the JVM.

What is a heap dump?

A heap dump is a snapshot of the memory of a Java process.

The snapshot contains information about the Java objects and classes in the heap at the moment the snapshot is triggered. Because there are different formats for persisting this data, there might be some differences in the information provided. Typically, a full garbage collection is triggered before the heap dump is written, so the dump contains information about the remaining objects in the heap.

reference: http://pic.dhe.ibm.com/infocenter/isa/v4r1m0/index.jsp?topic=%2Fcom.ibm.java.diagnostics.memory.analyzer.doc%2Fheapdump.html

I am mostly interested in creating heap dump in the program, I want to be able to create a heap dump every time a new cluster has been initialized. I followed the instructions at this blog post

https://blogs.oracle.com/sundararajan/entry/programmatically_dumping_heap_from_java

Note: there are certainly a number of ways to create a heap dump. I am using the approach that uses mxbeans api.

This is my test driver, I am trying to see how the size of an integer array would impact the objects size in the heap dump


public class testHeapDump {

private static int[] testArray;

public static void main(String[] args){

int arraySize = 1000000;

testArray = new int[arraySize];

for(int i = 0; i < arraySize; i++){

testArray[i] = i*2;

}

HeapDumper.dumpHeap("testHeapDump.bin", true);

}

}

I use visual vm to open the heap dump file, when I set the testArray size to 1000 (relatively small), the analysis looks like the following

testDump1-arraysize-1000

 

Notice the size of the array is small, only 2.8% of the overall heap,

 

when I set the testArray size to 1000000 (relatively large), the analysis looks like the following

testDump1-arraysize-1000000

 

As  you can see, the interesting part is int[], it occupies 78.4% of the overall heap size (the rightmost Size column). The size of int[] is 125 times the size of int[] in the previous heapDump file. It makes sense since the array  is 100 times longer.

 

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