Analyze Java Heap dump and Garbage collector with Eclipse

How to use MAT, MAT Easy! and GCVM plugin in Eclipse and analyze Heap dump and Garbage collector log.

Java is the most popular programming language in the world and there are many tools to analyze Web and Android applications developed with this language.

Many Web Application use Application Server JavaEE as Oracle Weblogic.

To begin analyzing a Java application, you need to collect some fundamental data:

  • Heap dump: the memory dumo of the JVM (Java Virtual Machine) .
  • Garbage collector log: “gc.log” where all JVM memory cleanup operations are tracked.
  • Thread dump: application thread dump.

The following commands have been launched on Oracle Java and Weblogic.

How to capture Java Heap dump.

Heap dump will help you observe objects in memory at a given time. So you can indentify tha cause of “out of memory”,”memory leak”, etc.
To do this, use the jmap command in the java binaries installation folder:

For dumping Heap memory in hprof binary format:

Up here, “pid” is the JVM ‘s Process ID or Weblogic application server.

How to enable Garbage collector log.

The Garbage collector log, or gc.log, is the text file where all the JVM memory cleanup events are stored: MinorGC, MajorGC e FullGC.

Launch JVM with this flags to create gc.log:

  • until Java 8
  • from Java 9

How to take thread dumps from a JVM.

A thread dump is a snapshot of the state of all threads that are part of the JVM’s process.
For Weblogic, lets you understand which “state” are the threads of the application server. Among the states the interesting:

  • STANDBY: the thread is waiting to perform an operation
  • ACTIVE: the thread is performing an operation and the stacktrace is reported
  • STUCK: the thread is labeled stuck after a certain amount of time set on the Weblogic domain admin. Potentially this thread may be blocked or is performing an operation that is taking longer than X minutes.

Syntax for generating thread dump:

How to analyze Heap dump e Garbage collector log with Eclipse.

To analyze the newly collected data, use the Eclipse ide and MAT, MAT Easy! and GCMV.

Official site to download Eclipse https://www.eclipse.org/downloads/.
Eclipse Marketplace to install the plugins:

How to use Eclipse perspective.

In order to use the plugins, you need to change the “perspective” of the Eclipse workspace.

come cambiare perspective in Eclipse

Go to Perspective menu.

GCMV perspective

come selezionare la perspective di GCMV

To analyze and create chart of Garbage collector log, open GCMV perspective.

MAT e MAT Easy! perspective

come selezionare la perspective di MAT e MAT Easy!

To analyze gli Heap dump, open Memory Analisys perspective.

GCMV – Analyzing Garbage Collector log.

In GCMV perspective, you can analyze and observe the chart of Garbage Collector operations.

esempio di garbage collector log graficato con GCMV

Drawing the garbage collection log graph is useful for identifying the behavior of the application. Among the most interesting aspects:

  • BLUE LINE: in this particular case you can see that the application has a memory saturation problem. From about 100mb goes to 400-500mb in a few minutes creating a ramp. The problem is not the growth of memory usage but the fact that it is no longer freed causing an Out of Memory (OOM).
  • AF LINE: Fuchsia points identify MinorGC (Young generation) cleaning operations.
  • FULL LINE: Fuchsia points identify FullGC (Young – Old) cleaning operations.
  • RED LINE: the duration of garbage collection (Minor or Full).

MAT – Analyzing Heap dump.

This is the first screen that will appear to you as soon as you have finished parsing the hprof dump. The pie chart helps you identify large objects (181 mb) in relation to the size of the Heap (512mb in this case).

Eclipse visualizza oggetti contenuti nell' heap dump

The “Leak suspect” parsing automatically identified a Thread in STUCK, containing the object macro highlighted above.

Problem Suspect รจ il risultato dell'analisi Leak Suspect

In the Dominator tree and sorting by Retained Heap, you can notice the weblogic.work.ExecuteThread object ‘4’ in STUCK. The same is reported by the Leak Suspect analysis.

Thread in Stuck nel Dominator Tree

MAT Easy! – Easy analizing Heap dump

MAT Easy! makes it easier to see the data in the Dominator tree.

Classi Java nel Dominator Tree

Click Collection Tree button to use MAT Easy!

MAT Easy! Collection Tree analisys

With the Collection Tree, you can more easily analyze the contents of the dominator tree. You can traverse objects more easily until you get to the contents of individual objects.

Good analysis of your Java applications! ๐Ÿ˜‰