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:

herong> javac GarbageCollection.java

herong> java GarbageCollection
Free/total memory:
23725256   25034752
22710400   25034752
21618728   25034752
20523584   25034752
...

(Start another command window.)
herong> jps -l -m
2288 jdk.jcmd/sun.tools.jps.Jps -l -m
10012 GarbageCollection

herong> jmap -histo 10012

  num   #instances     #bytes  class name (module)
--------------------------------------------------
   1:         2928   27043688  [I (java.base@10.0.1)
   2:           80   19663104  [J (java.base@10.0.1)
   3:           75   19399856  [D (java.base@10.0.1)
   4:       302762    7266288  java.lang.String (java.base@10.0.1)
   5:         3319    4951872  [B (java.base@10.0.1)
   6:           74    4785312  [F (java.base@10.0.1)
   7:           86    4631088  [C (java.base@10.0.1)
   8:           88    1165048  [Ljava.lang.String; (java.base@10.0.1)
   9:         1478      97528  [Ljava.lang.Object; (java.base@10.0.1)
  10:          702      85984  java.lang.Class (java.base@10.0.1)
  11:         2398      76736  java.util.HashMap$Node (java.base@10.0.1)
  12:         1679      53728  java.util.concurrent.ConcurrentHashMap$N...
  13:          451      45376  [Ljava.util.HashMap$Node; (java.base@10....
  14:          477      22896  java.util.HashMap (java.base@10.0.1)
  15:           24      15616  [Ljava.util.concurrent.ConcurrentHashMap...
  16:          554      13296  java.lang.module.ModuleDescriptor$Export...
  17:          391      12512  java.lang.invoke.LambdaForm$Name (java.b...
  18:          227      10896  java.lang.invoke.MemberName (java.base@1...
  19:          146       9344  [Ljava.lang.ref.SoftReference; (java.bas...
  20:          242       7744  java.lang.module.ModuleDescriptor$Requir...
  21:          220       7040  java.lang.invoke.MethodType$ConcurrentWe...
  22:          173       6920  java.lang.invoke.MethodType (java.base@1...
  23:           80       6400  java.net.URI (java.base@10.0.1)
  24:          265       6360  java.util.ImmutableCollections$SetN (jav...
  25:          174       6144  [Ljava.lang.Class; (java.base@10.0.1)
  ...
Total       323237   89521384

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

Table of Contents

 About This Book

 Java Tools Terminology

 Java Tools Included in JDK

 javac - The Java Program Compiler

 java - The Java Program Launcher

 jar - The JAR File Tool

 jlink - The JRE Linker

 jmod - The JMOD File Tool

 jimage - The JIMAGE File Tool

 jpackage - Binary Package Builder

 javadoc - The Java Document Generator

 jdeps - The Java Class Dependency Analyzer

 jdeprscan - The Java Deprecated API Scanner

 jdb - The Java Debugger

 jcmd - The JVM Diagnostic Tool

 jconsole - Java Monitoring and Management Console

 jstat - JVM Statistics Monitoring Tool

JVM Troubleshooting Tools

 JVM Troubleshooting Tools in JDK

 jinfo - VM Option Value Checker

 jinfo - Changing HotSpot VM Option

 jstack - Stack Tracer to Generate Thread Dump

 What Is a Thread Dump?

 Java Thread Deadlock Demo Program

 jstack - Detecting Java Thread Deadlocks

 jmap - JVM Heap Dump Tool

Printing Histogram of Java Object Heap

 jmap - Generating Heap Dump File

 jhat - Java Heap Analysis Tool

 jhat - Starting 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

 jhsdb - The Java HotSpot Debugger

 jvisualvm (Java VisualVM) - JVM Visual Tool

 jmc - Java Mission Control

 javap - The Java Class File Disassembler

 keytool - Public Key Certificate Tool

 jarsigner - JAR File Signer

 jshell - Java Language Shell

 jrunscript - Script Code Shell

 Miscellaneous Tools

 native2ascii - Native-to-ASCII Encoding Converter

 JAB (Java Access Bridge) for Windows

 Archived Tutorials

 References

 Full Version in PDF/EPUB