Java Tool Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 5.11

Printing Histogram of Java Object Heap

This section provides a tutorial example on how to print the histogram of Java object heap, a list of different types of objects and their counts and total sizes.

The first function of the "jmap" tool is to print histogram of object heap of a given JVM process. Now I am going to use a sample Java program, GarbageCollection.java, I wrote in another tutorial example in this book.

The first test is to print the heap histogram of a JVM process that runs GarbageCollection.java with the "jmap -histo pid" command:

C:\herong>\Progra~1\java\jdk1.6.0_02\bin\javac GarbageCollection.java


C:\herong>\Progra~1\java\jdk1.6.0_02\bin\java 
   -Xms24m -Xmx24m GarbageCollection

Free/total memory:
23725256   25034752
22710400   25034752
21618728   25034752
20523584   25034752
...


(Start another command window.)
C:\herong>\Progra~1\java\jdk1.6.0_02\bin\jps -l -m

492 sun.tools.jps.Jps -l -m
428 GarbageCollection


C:\herong>\Progra~1\java\jdk1.6.0_02\bin\jmap -histo 428

num   #instances    #bytes  class name
--------------------------------------
  1:        23     4723136  [I
  2:        19     4718928  [J
  3:        18     4718880  [D
  4:     73925     1774200  java.lang.String
  5:       208     1226400  [C
  6:        28     1205064  [B
  7:        18     1179936  [F
  8:        68      297040  [Ljava.lang.String;
  9:       332       14136  [Ljava.lang.Object;
 10:        32       10240  <objArrayKlassKlass>
 11:        42        4032  java.lang.Class
 12:        58        1392  java.util.Hashtable$Entry
 13:        16        1280  [Ljava.util.HashMap$Entry;
 14:        27        1000  <symbolKlass>
 15:         6         864  [S
 16:         7         680  [Ljava.util.Hashtable$Entry;
 17:        11         616  java.net.URL
 18:        19         608  java.util.Locale
 19:         5         560  java.lang.Thread
 20:        14         560  java.util.HashMap
...

The histogram gives a very good summary of heap objects used in my GarbageCollection.java program:

  • Most of the objects were String objects. There were 73925 of them. This matches well with what I allocated in the program: one array of 64*64 string objects for each 1-MB object. If 16 1-MB objects were allocated, there should be 65536 string objects.
  • "[Ljava.lang.String;" is a special class name representing a java.lang.String[] array.
  • "[I" is a special class name representing a int[] array.

Sections in This Chapter

JVM Troubleshooting Tools in JDK 1.5

'jinfo' - VM Option Value Checker

Changing HotSpot VM Option using 'jinfo'

'jstack' - Stack Tracer of JVM Threads

Java Thread Deadlock Demo Program

Detecting Java Thread Deadlocks with 'jstack'

'jmap' - JVM Heap Dump Tool

Printing Histogram of Java Object Heap

Generating Heap Dump File with 'jmap'

'jhat' - Java Heap Analysis Tool

Starting 'jhat' Web Server on a Heap Dump File

Listing Instance Counts of All Classes

Browsing Object Instance Values

Object Query Language (OQL)

Searching for Instances with OQL Statements

Dr. Herong Yang, updated in 2008
Printing Histogram of Java Object Heap