In my previous post on heap histograms, I missed another way to view histogram. Thanks to Alan Bateman for pointing out the same in his comment! Here it is:
The fifth way! Use the heapViewer demo JVM TI agent shipped with JDK.
On Windows:
java -agentpath:%JAVA_HOME%\demo\jvmti\heapViewer\lib\heapViewer.dll MainClass
On Unix:
java -agentpath:$JAVA_HOME/demo/jvmti/heapViewer/lib/libheapViewer.so MainClass
where JAVA_HOME is the directory where your JDK is installed. This heapViewer agent prints histogram whenever SIGQUIT signal is sent to the Java process or when the Java process exits (normal exit or Ctrl-C). Sample output of the heapViewer agent is in this blog entry by Alan.
You can look at the source code of this heapViewer agent under $JAVA_HOME/demo/jvmti/heapViewer/src directory. If you want to learn about JVM TI agents, you may want to read Kelly's blog on agents.
With Mustang (Java SE 6), there are atleast 4 ways to get histogram of Java heap:
jmap -histo <pid-of-java-process>
jmap -dump:format=b,file=heap.bin <pid-of-java-process>
jhat heap.bin
When jhat is started, visit the URL "http://localhost:7000/" and click on "Show heap histogram" link to view the histogram.
hotspot$1:::object-alloc {
self->str_ptr = (char*) copyin(arg1, arg2+1);
self->str_ptr[arg2] = '\0';
self->classname = (string) self->str_ptr;
@allocs_count[self->classname] = count();
@allocs_size[self->classname] = sum(arg3);
}