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.8.0\bin\javac GarbageCollection.java


C:\herong>\Progra~1\java\jdk1.8.0\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.8.0\bin\jps -l -m

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


C:\herong>\Progra~1\java\jdk1.8.0\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:

Last update: 2015.

Table of Contents

 About This Book

 Java Tools Terminology

 Installing Java 8 on Windows

 'javac' - The Java Program Compiler

 'java' - The Java Program Launcher

 'jdb' - The Java Debugger

 'jconsole' - Java Monitoring and Management Console

 'jstat' - JVM Statistics Monitoring Tool

JVM Troubleshooting Tools

 JVM Troubleshooting Tools in JDK 1.5

 'jinfo' - VM Option Value Checker

 Changing HotSpot VM Option using 'jinfo'

 'jstack' - Stack Tracer to Generate Thread Dump

 What Is a Thread Dump?

 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

 jvisualvm (Java VisualVM) - JVM Visual Tool

 'jar' - The JAR File Tool

 'javap' - The Java Class File Disassembler

 'keytool' - Public Key Certificate Tool

 'native2ascii' - Native-to-ASCII Encoding Converter

 Outdated Tutorials

 References

 PDF Printing Version