SoFunction
Updated on 2025-03-02

Causes and solutions for the heap memory overflow of Java virtual machine

Case background

In "In-depth understanding of Java Virtual Machines: Advanced Features and Best Practices for JVM (3rd Edition), the author uses a simple example to illustrate the memory overflow problem. This example triggers the ArrayList by continuously adding objects to an ArrayList until memory runs outOutOfMemoryError

Code Analysis

import ;
import ;

public class HeapOOM {

    static class OOMObject {
    }

    public static void main(String[] args) {
        List<OOMObject> list = new ArrayList<OOMObject>();

        while (true) {
            (new OOMObject());
        }
    }
}

This code creates aArrayList, and then continue to add to itOOMObjectExample. becauseOOMObjectNo logic is implemented, it is just an empty class and takes up very little memory. However, since there is no limit on the number of added objects, it will eventually lead to memory exhaustion.

JVM parameter settings

To better understand the process of memory overflow, we can set JVM parameters to control the size of heap memory and generate a heap dump file when a memory overflow occurs:

-Xms20m -Xmx20m -XX:+HeapDumpOnOutOfMemoryError
  • -Xms20m: Set the initial heap memory at JVM startup to 20MB.
  • -Xmx20m: Set the maximum heap memory that the JVM can use is 20MB.
  • -XX:+HeapDumpOnOutOfMemoryError: Generate a heap dump file when a memory overflow occurs.

Memory overflow analysis

When running the above code, the JVM will continuously allocate memory to store newOOMObjectExample. Since heap memory is limited to 20MB, the JVM will try to garbage collection when the memory is exhausted. If you still cannot find enough space to allocate a new object after garbage collection, it will be thrownOutOfMemoryError

error message

: Java heap space
Dumping heap to java_pid7696.hprof ...
Heap dump file created [28354274 bytes in 0.081 secs]
Exception in thread "main" : Java heap space
	at (:3210)
	at (:3181)
	at (:261)
	at (:235)
	at (:227)
	at (:458)
	at (:19)
  • : Java heap space: Indicates that the Java heap memory space is insufficient.
  • Dumping heap to java_pid7696.hprof: means that the JVM has generated a heap dump file, the file name isjava_pid7696.hprof
  • Heap dump file created [28354274 bytes in 0.081 secs]: Indicates that the heap dump file was created successfully, with a size of 28MB.

Heap dump analysis

By analyzing the heap dump file, we can view the distribution of objects in memory and find out areas where memory leaks or overuse is available. In this case, we can see a lot ofOOMObjectInstances, these are all caused by infinite loops in the code.

Solution

  • Limit the number of objects: Set an upper limit when adding objects to avoid infinite loops.
  • Increase heap memory: If you really need to process a large amount of data, you can consider increasing the heap memory size of the JVM.
  • Optimize code logic: Check the code logic to ensure that there are no unnecessary object creation.

Summarize

Memory overflow is a common problem in Java development. By reasonably setting JVM parameters and code optimization, this problem can be effectively avoided and solved. Hopefully this case can help you better understand the causes and solutions of memory overflow.

This is the end of this article about the case analysis of Java virtual machine heap overflow. For more related Java virtual machine heap overflow content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!