Java heap memory overflow
As long as you constantly create objects and ensure that there is an accessible path between the GC roots and objects to avoid the garbage collection mechanism clearing these objects,
The memory overflow exception occurs when the number of objects reaches the maximum heap capacity limit. The Code is as follows:
import java.util.ArrayList;import java.util.List;public class HeapOOM { static class OOMObject { } public static void main(String[] args) { List<OOMObject> list = new ArrayList<>(); while (true) { list.add(new OOMObject()); } }}
During running, you need to set the Java heap size, which cannot be expanded. The operation is as follows:
Then fill in the JVM startup parameter-xms20m-xmx20m-XX: + heapdumponoutofmemoryerror
The size of the Java heap is limited to 20 mb. the maximum and minimum values are the same and cannot be extended. The-XX: + heapdumponoutofmemoryerror parameter allows the virtual machine to appear
When a memory overflow exception occurs, dump the current memory dump snapshot for later analysis. (If you are interested, you can use the memory image analysis tool eclipse memory analyzer to open the heap dump snapshot file for analysis)
The running result is as follows:
Outofmemoryerror/OOM/memory overflow exception instance analysis-Heap Memory exception