<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Krishna</title>
    <description>The latest articles on DEV Community by Krishna (@sbvkrishna).</description>
    <link>https://dev.to/sbvkrishna</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F225731%2Fcaedd29d-625e-4610-9abb-d2eb0f302f16.jpeg</url>
      <title>DEV Community: Krishna</title>
      <link>https://dev.to/sbvkrishna</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sbvkrishna"/>
    <language>en</language>
    <item>
      <title>Debugging Java Memory issues: Thread and Heap Dump Analysis</title>
      <dc:creator>Krishna</dc:creator>
      <pubDate>Sun, 12 Sep 2021 17:42:44 +0000</pubDate>
      <link>https://dev.to/sbvkrishna/debugging-java-memory-issues-thread-and-heap-dump-analysis-43c5</link>
      <guid>https://dev.to/sbvkrishna/debugging-java-memory-issues-thread-and-heap-dump-analysis-43c5</guid>
      <description>&lt;p&gt;Java has garbage collection (GC) feature which automatically handles memory management for us - determines what memory is no longer being used by a Java application, destroying/deallocation the unused objects and reclaiming the runtime unused memory for reuse.&lt;br&gt;
Even though GC makes Java memory-efficient, we often fall prey to crippling memory leaks which impacts the software performance in unpredictable ways and can even bring down an entire system.&lt;/p&gt;

&lt;p&gt;Monitoring the JVM’s GC activity with Memory &amp;amp; Heap usage and collecting the Thread/Heap Dumps when required can help identify and root cause common Java memory issues.&lt;br&gt;
Usually, memory leaks show up as OutOfMemoryError (OOM), but not necessarily always. For example, if the Heap size is too small and if the applcation is trying to allocate memory for new objects but it breached the max heap size configured, the error would look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;java.lang.OutOfMemoryError: Java heap space
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Working with Java Heap Dumps
&lt;/h2&gt;

&lt;p&gt;Java uses the heap as the storage for Java objects. Collecting and analysing the Heap Dumps is great way to identify and root cause lot of memory related issues. &lt;/p&gt;

&lt;h3&gt;
  
  
  Collecting Heap Dumps
&lt;/h3&gt;

&lt;p&gt;Java has a built-in feature for dumping heap snapshots to files in HPROF binary format. We can create &lt;code&gt;.hprof&lt;/code&gt; memory snapshots on demand or automatically configure the programs to create Heap dumps, which can help with uncovering inefficient heap usage and debugging memory leaks.&lt;/p&gt;

&lt;p&gt;We can collect Heap Dumps for a Java application through:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Automatically on OutOfMemoryError&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the Java application is configured the VM flag &lt;code&gt;-XX:+HeapDumpOnOutOfMemoryError&lt;/code&gt;, then a heap dump is collected on the first OOM Error.
Note that there will not be any overhead involved unless an OOM actually occurs, so it's recommended to enable this for all production systems which can be handy if and when any memory issue comes.&lt;/li&gt;
&lt;li&gt;It's important to note that the dump file can be huge, up to Gigabytes, so ensure that the target file system has enough space.
If the application hits OOM Error frequently and is creating Heap dumps often, the disk space might run out of space soon causing many more cascading issues!&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;On-demand via jmap utility (OracleJDK/OpenJDK HotSpot)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the Java &lt;code&gt;bin&lt;/code&gt; directory, you can use &lt;code&gt;jmap&lt;/code&gt; utility to get a live heap dump and/or dump the heap on OOM with a JVM argument.&lt;/li&gt;
&lt;li&gt;Find the ProcessId (PID) for the Java application by running &lt;code&gt;ps aux | grep java&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Run below &lt;code&gt;jmap&lt;/code&gt; command to generate the Heap dump for given process with &lt;code&gt;PID&lt;/code&gt; with given .&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;jmap -F -dump:format=b,file=&amp;lt;file_name&amp;gt;.hprof &amp;lt;PID&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Analysing Heap Dumps
&lt;/h3&gt;

&lt;p&gt;The Heap Dumps collected are not readable directly. After collecting the Heap Dump, we would need to parse the Heap Dump file for analysis to produce a readable and easy-to-understand report.&lt;br&gt;
An Analyzer can help to quickly calculate the retained sizes of objects, see who/what is preventing the Garbage Collector from collecting objects and identify memory leak suspects.&lt;br&gt;
There are multiple Analyzer tools available to do this - Eclipse MAT is one such tool.&lt;/p&gt;
&lt;h4&gt;
  
  
  Using Eclipse MAT
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://www.eclipse.org/mat/"&gt;Eclipse Memory Analyzer (MAT)&lt;/a&gt; is a fast and feature-rich Java heap analyzer that helps you find memory leaks and reduce memory consumption.&lt;br&gt;
To analyse Heap Dumps (even very large ones) using Eclipse MAT, you can follow below steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Install JDK11 (or newer)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you have &lt;a href="https://brew.sh/"&gt;Homebrew&lt;/a&gt; and would like to install &lt;a href="https://aws.amazon.com/corretto/"&gt;Amazon Corretto&lt;/a&gt; (free OpenJDK distribution), you can run &lt;code&gt;brew tap homebrew/cask-versions &amp;amp;&amp;amp; brew install --cask corretto&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;To verify the installation if on MacOS, run &lt;code&gt;/usr/libexec/java_home -V&lt;/code&gt;. This will print out the directory path to which the JDK is installed to.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Install Eclipse MAT&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Install Eclipse MAT by following the &lt;a href="https://wiki.eclipse.org/MemoryAnalyzer#Installation"&gt;Official Installation guide&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Assuming Eclipse MAT is installed and we are inside the &lt;code&gt;mat/&lt;/code&gt; directory, modify &lt;code&gt;MemoryAnalyzer.ini&lt;/code&gt; file settings to use a large heap to handle large dumps and add the Java bin path to &lt;code&gt;vm&lt;/code&gt; option.

&lt;ul&gt;
&lt;li&gt;Here, Xms specifies the minimum Heap memory and Xmx specifies the maximum.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://wiki.eclipse.org/Eclipse.ini#Specifying_the_JVM"&gt;Reference wiki for configuring VM options&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-vm
/usr/lib/jvm/java-11-amazon-corretto.x86_64/bin/java
-startup
plugins/org.eclipse.equinox.launcher_1.6.200.v20210416-2027.jar
--launcher.library
plugins/org.eclipse.equinox.launcher.gtk.linux.x86_64_1.2.200.v20210429-1609
-vmargs
-Xms24g
-Xmx24g
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Run MAT against the heap dump.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./ParseHeapDump.sh /tmp/jvm.hprof
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;This takes a while to execute and generates the &lt;code&gt;index&lt;/code&gt; files and other files to make repeated analysis faster.&lt;/p&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;Then use the &lt;code&gt;index&lt;/code&gt; file created in the previous step and run a "Leak suspects" report on the heap dump.&lt;br&gt;
This creates a small output file &lt;code&gt;jvm_Leak_Suspects.zip&lt;/code&gt; which would have easy to view HTML report with the memory leak suspects.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./ParseHeapDump.sh /tmp/jvm.hprof org.eclipse.mat.api:suspects
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Other Alternative Heap Dump Analysis Tools
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://visualvm.github.io/"&gt;VisualVM&lt;/a&gt; : A GUI Java profiling/analysis tool that can be used for Performance profiling (including per-thread analysis), Thread/Heap dumping, Monitoring.

&lt;ul&gt;
&lt;li&gt;For JDK 8, this comes bundled as &lt;code&gt;jvisualvm&lt;/code&gt;. For JDK 9, you will need to manually download it.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.ej-technologies.com/products/jprofiler/overview.html"&gt;JProfiler&lt;/a&gt; [Paid] : JProfiler is a Java profiler combining CPU, Memory and Thread profiling in one application.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Working with Thread Dumps
&lt;/h2&gt;

&lt;p&gt;A thread dump is a snapshot of the state of all the threads of a Java application process.&lt;br&gt;
A thread dump can be very useful for diagnosing/debugging problems related to threads, like high CPU usage etc.&lt;/p&gt;
&lt;h3&gt;
  
  
  Collecting Thread Dumps
&lt;/h3&gt;

&lt;p&gt;Java JDK provides multiple tools for capturing the thread dump of a Java application which are located under the &lt;code&gt;bin&lt;/code&gt; folder inside the JDK home directory.&lt;br&gt;
One such tool is &lt;code&gt;jstack&lt;/code&gt; which can be used by following below steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Find the ProcessId (PID) for the Java application by running &lt;code&gt;ps aux | grep java&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Run &lt;code&gt;jstack&lt;/code&gt; with given &lt;code&gt;PID&lt;/code&gt; and given FileName (like &lt;code&gt;/tmp/thread_dump_1.txt&lt;/code&gt;).&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;jstack -l &amp;lt;PID&amp;gt; &amp;gt; /tmp/thread_dump_1.txt
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The output stored in the file is plain text and can be viewed in a simple text editor as well.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Identifying threads consuming high CPU
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Capture the Thread dump of the Java applcation using above steps (if not done already).&lt;/li&gt;
&lt;li&gt;Find the processId (PID) for the application by running &lt;code&gt;ps aux | grep java&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Identify the threadIds consuming high CPU for the given processId PID from previous step.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;top -n 1 -H -p &amp;lt;PID&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Convert the threadId to corresponding Hexadecimal value using some &lt;a href="https://www.rapidtables.com/convert/number/decimal-to-hex.html"&gt;Decimal to Hexadecimal converter&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Lookup the Hex value of threadId in thread dump output file to identify the name and stack trace of desired thread.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Debugging Garbage Collection issues
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Garbage Collection logs
&lt;/h3&gt;

&lt;p&gt;The Garbage Collection logs can record memory usage information and garbage collection related performance metrics (throughput, accumulated pauses, longest pause, etc.) that can be analyzed using tools like &lt;a href="http://www.tagtraum.com/gcviewer.html"&gt;GCViewer&lt;/a&gt;.&lt;br&gt;
Enabling GC logs in our software systems can come in handy when things go wrong incase issues like Memory leaks.&lt;/p&gt;

&lt;p&gt;Below are some common JVM flags used for Garbage Collection logging in Java 8. Refer to &lt;a href="https://www.baeldung.com/java-gc-logging-to-file"&gt;https://www.baeldung.com/java-gc-logging-to-file&lt;/a&gt; for more info.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"-XX:+PrintGCDetails"
"-XX:+PrintGCDateStamps"
"-XX:+PrintTenuringDistribution"
"-XX:+PrintClassHistogram"
"-Xloggc:gc_log_file.log"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;blockquote&gt;
&lt;p&gt;Cross-posted to &lt;a href="https://sbvkrishna.tech/debugging-java-memory-issues"&gt;https://sbvkrishna.tech/debugging-java-memory-issues&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Java Garbage Collection

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://mechanical-sympathy.blogspot.com/2013/07/java-garbage-collection-distilled.html"&gt;https://mechanical-sympathy.blogspot.com/2013/07/java-garbage-collection-distilled.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.toptal.com/java/hunting-memory-leaks-in-java"&gt;https://www.toptal.com/java/hunting-memory-leaks-in-java&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.baeldung.com/java-gc-logging-to-file"&gt;https://www.baeldung.com/java-gc-logging-to-file&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Analyzing &lt;code&gt;hprof&lt;/code&gt; file

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.yourkit.com/docs/java/help/hprof_snapshots.jsp"&gt;https://www.yourkit.com/docs/java/help/hprof_snapshots.jsp&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.ashwinjayaprakash.com/2013/11/analyzing-large-java-heap-dumps-when.html"&gt;https://www.ashwinjayaprakash.com/2013/11/analyzing-large-java-heap-dumps-when.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/185893/how-do-i-analyze-a-hprof-file"&gt;https://stackoverflow.com/questions/185893/how-do-i-analyze-a-hprof-file&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>java</category>
      <category>debugging</category>
    </item>
  </channel>
</rss>
