Java heap analysis

Updated at:

This article explains how to use the java heap analysis feature of ATP to find the cause of an out of memory error in your application.

1. Generate and upload data source

When you encounter the following out of memory error in a production environment:

java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-3" Exception in thread "Thread-1" java.lang.OutOfMemoryError: Java heap space
java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "Thread-6" java.lang.OutOfMemoryError: Java heap space

When this happens, generate a heap dump file for the application (see Get started with ATP for instructions), upload it to ATP, and start the analysis.

2. Analyze heap data

On the analysis results page, first check the Overview Report to view information such as the heap size, generation time, and JDK version. This helps you rule out operational errors and confirm that the file matches the application you want to analyze.

The Basic Information section shows that the operating system is Mac OS X and the JDK version is 11.0.8. The Key Metrics section reveals a total live object size of 3.76 GB, 90.01 MB of unreachable objects, 3.85 GB of heap usage, 902 classes, 12 threads, and 4 class loaders.

The Object Report tab shows that class App @ 0x716500ed0 consumes 99.9% of the heap. However, this is a class, and a class itself does not consume much memory. To investigate further, click Outgoing References to see what this class references:

The App class has a ConcurrentHashMap static field that holds 3.76 GB of memory.

Expand the ConcurrentHashMap node. Its key is of type Integer and its value (val) is of type App$Holder. The pane on the right shows that the App$Holder object has two fields: data and processed.

Next, click the class App object. The right pane shows that it has a static field named queue, which is of type ConcurrentHashMap and holds approximately 3.76 GB of memory.

3. Conclusion

From this information, you can deduce what the problematic Java code might look like:

class App {
    static class Holder {
        public boolean processed;
        byte[] data = new byte[1024 * 1024 * 50];
    }
    // 3.76GB
    static ConcurrentHashMap<Integer, Holder> queue = new ConcurrentHashMap<>();
}

By cross-referencing this information with your application's source code, you can pinpoint the problem's cause.