Heap Memory Leak
Fault Locating
The Java program breaks down or responds slowly. The message "OutOfMemoryError: Java heap space" exists in the log file. Figure 1 shows how to locate and rectify the fault.
- Confirm the out of memory (OOM) problem by checking the error logs, memory usage, and GC activities.
- Increase the memory parameter value to check whether memory leak occurs. If the problem does not recur after the memory parameter value is increased, the problem is not caused by memory leak.
- Export the stack file using jmap and analyze the file based on the object call stack.
- Check whether the space usage of large-memory objects is proper.
- Export stack data before and after service invoking. Compare the object memory usage and analyze whether the memory usage increase is proper.
- If the code is incorrect, modify and recompile the code. Then, perform a test and verification.
- If it is not a memory leak, check whether it is a JVM fault.
Case: Memory Leak Occurs Due to Periodic Addition of String Objects
Symptom
A Java process runs slowly and the request delay is abnormally high. The "OutOfMemoryError: Java heap space" exception is recorded in logs.
Fault Locating
- Run the jstat command to check GC activities. The command output shows that full GC occurs frequently and the memory usage of the old generation (column O) is close to 100%.

- Increase the maximum heap memory and run the service again. After a period of time, check the GC activity. The GC is still in full load.
- Use jmap to capture the memory. It is found that a large number of string objects exist.

- Use jmap to export the heap memory file and use the jvisualvm tool to load the file for further analysis. Analyze possible causes based on the code.

- According to the code logic analysis, the Map object in this case periodically adds data but does not clear the data. Therefore, the fault can be located.

- Compile the code and install the patch in the environment after the code is modified. The problem is resolved.
Parent topic: Troubleshooting
