Heap dump is a snapshot of the Java memory. It contains information about the Java objects and classes in the heap at the moment the snapshot is triggered. It’s vital artifact to diagnose any Java memory related problems

Thread dumps are vital artifacts to diagnose CPU spikes, deadlocks, poor response times, memory problems, unresponsive applications, and other system problems. There are great online thread dump analysis tools such as fastthread, which can analyse and spot problems.

1 Identify the Process Id

The first piece of information you will need to be able to obtain a thread dump is your Java process's PID.

Copy
ps -ef |grep jetty-runner

2 Debug memory usage

2.1 The jstat Utility

The jstat utility uses the built-in instrumentation in the Java HotSpot VM to provide information about performance and resource consumption of running applications. The tool can be used when diagnosing performance issues, and in particular issues related to heap sizing and garbage collection. The jstat utility does not require the VM to be started with any special options. The built-in instrumentation in the Java HotSpot VM is enabled by default. This utility is included in the JDK download for all operating system platforms supported by Oracle

The following exmaple illustrates the use of -gcutil option where the jstat utility attaches to vmid number ${VMID}, takes seven samples at 250-millisecond intervals.

Copy
jstat -gcutil ${VMID} 250 7

output

Copy
S0     S1     E      O      M     YGC     YGCT    FGC    FGCT     GCT   
  0.00  99.74  13.49   7.86  95.82      3    0.124     0    0.000    0.124
  0.00  99.74  13.49   7.86  95.82      3    0.124     0    0.000    0.124
  0.00  99.74  13.49   7.86  95.82      3    0.124     0    0.000    0.124
  0.00  99.74  13.49   7.86  95.82      3    0.124     0    0.000    0.124
  0.00  99.74  13.49   7.86  95.82      3    0.124     0    0.000    0.124
  0.00  99.74  13.49   7.86  95.82      3    0.124     0    0.000    0.124
  0.00  99.74  13.49   7.86  95.82      3    0.124     0    0.000    0.124

To visualize the output visit the following site or use the visualgc tool

2.2 Heap Dump

2.2.1 Capture Heap Dump

You can use the jmap tool to capture the heap dump. jmap prints heap memory details of a given process into a file. jmap tool is shipped with JDK. Here is how you should invoke it:

Copy
jmap -dump:format=b,file=<file-path> <pid>

where

  • pid: is the Java Process Id, whose heap dump should be captured
  • file-path: is the file path where heap dump will be written in to.

2.2.2 Automatic heap dump on low memory condition

You can also capture Heap Dump when JVM experienced OutOfMemoryError by passing ‘-XX:+HeapDumpOnOutOfMemoryError’ system property. This property is quite an effective property that I would recommend all JVMs to have this property configured. Due to heat of the moment, sometimes Operations team might forget to capture heap dump. It’s extremely hard to diagnose any memory problems without heap dumps. This Property can be the savior of the day in such circumstances.

Copy
-XX:+HeapDumpOnOutOfMemoryError

3 Thread Dump

jstack is an effective command line tool to capture thread dumps. jstack tool is shipped in JDK_HOMEbin folder. Here is the command that you need to issue to capture thread dump:

Copy
jstack -l  <pid> > <file-path>

where:

  • pid: is the Process Id of the application, whose thread dump should be captured
  • file-path is the file path where thread dump will be written in to.
Example

Copy
jstack -l 37320 > /home/user/threadDump.txt

The thread dump of the process would be generated in /home/user/threadDump.txt .

4 Remote debug

If you have a web application deployed into Jetty you can interact with it remotely from a debugging perspective easily. The basics are that you must start up the remote JVM with additional parameters and then start up a remote debugging session in Eclipse or Intellij for the webapp.

Copy
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=6006

For instance, a start shell with debug enabled would look like:

Copy
/opt/jdk1.8.0_60/bin/java -Djava.net.preferIPv4Stack=true -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=6006  -XX:+HeapDumpOnOutOfMemoryError -jar bin/jetty-runner-9.3.9.v20160517.jar --port 8081 libs/axional.rf.web-0.0.459-SNAPSHOT.war

4.1 Linking with VisualVM or IDE

VisualVM is a visual tool integrating commandline JDK tools and lightweight profiling capabilities.

Open VisualVM tool and select the "Remote" option on the left side menu and then select the option "Add new host"

And enter the host URL and the port.

4.2 Linking withr IDE

Refer to the documentation for your ide: