• Uncategorised

How To Solve OutOfMemoryError: Java Heap Space

Sample Program Generating ‘OutOfMemoryError: Java heap space’

To better understand java.lang.OutOfMemoryError: Java heap space, let’s try to simulate it. Let’s leverage BuggyApp,  a simple open-source chaos engineering project. BuggyApp can generate various sorts of performance problems such as Memory Leak, Thread Leak, Deadlock, and multiple BLOCKED threads. Below is the program from the BuggyApp project that can simulate java.lang.OutOfMemoryError: Java heap space when executed.

public class MapManager {      
    
    private static HashMap<Object, Object> myMap = new HashMap<>();      
    
    public void grow() {            
       long counter = 0;      
       while (true) {               

       if (counter % 1000 == 0) {                        
          System.out.println("Inserted 1000 Records to map!");        
       }                 
       myMap.put("key" + counter, "Large stringgggggggggggggggggggggggg" + counter);                  
       ++counter;      
    }   
   }
 }

The above program has a MapManager class that internally contains a HashMap object that is assigned to the myMap variable. Within the grow() method, there is an infinite while (true) loop that keeps populating the HashMap object. On every iteration, a new key and value (i.e., key-0 and Large stringgggggggggggggggggggggggg-0) is added to the HashMap. Since it’s an infinite loop, myMap object will get continuously populated until heap capacity is saturated. Once the heap capacity limit is exceeded, the application will result in java.lang.OutOfMemoryError: Java heap space

You need to capture the heap dump from the application, right before JVM throws OutOfMemoryError. In this post, 8 options to capture the heap dump are discussed. You might choose the option that fits your needs. My favorite option is to pass the -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=<FILE_PATH_LOCATION> JVM arguments to your application at the time of startup. 

Analyze Heap Dump

Once a heap dump is captured, you need to use tools like HeapHero, JHat, etc. to analyze the dumps.

How To Analyze Heap Dump?

In this section let’s discuss how to analyze heap dump using the HeapHero tool.

HeapHero is available in two modes:

  1. Cloud: You can upload the dump to the HeapHero cloud and see the results.
  2. On-Prem: You can register to get the HeapHero installed on your local machine and then do the analysis.

Note: I prefer using the on-prem installation of the tool instead of using the cloud edition because heap dump tends to contain sensitive information (such as SSN, Credit Card Numbers, VAT, etc.), and don’t want the dump to be analyzed in external locations.

Once the tool is installed, upload your heap dump to the HeapHero tool. The tool will analyze the dump and generate a report. Let’s review the report generated by the tool for the above program.

Above is the “Largest Objects” section screenshot from the HeapHero’s heap dump analysis report. This section shows the largest objects that are present in the application. In the majority of cases, the top 2–3 largest objects in the application are responsible for Memory Leak in the application.

If you want to see who created or holding on to the reference of the largest objects, you can use the “Incoming References” feature of the tool. When this option is selected, it will display all the objects that are referencing the MapManager class.

From this report you can notice that MapManager is referenced by Object2, which is in turn referenced by Object1, which in turn is referenced by MemoryLeakDemo.

You may also like...