Let's look at the Java heap:
For the allocation of the Java memory area, you can view an article in the Java Runtime data region.
The Java heap is the largest chunk of virtual machine memory management, which is thread-shared, and all threads in a Java process can access the data in that zone. Most areas of the Java heap are stored instance objects, which we often call objects in Java object-oriented programming. For example, I created a class that, through the constructor of this class, new object, the new object is stored on the Java heap. It can be subdivided into the new generation and the old age according to the time of the object generation and the reference time.
The Java heap size is controlled by the-XMS and-XMX parameters when the virtual machine starts. If the instance object is not allocated on the Java heap and the heap cannot be extended, an OutOfMemoryError exception will be thrown.
Next, simulate the overflow of the Java heap. Here is the test code:
Packagecom.woasis.jvm.classloading;Importjava.util.ArrayList;Importjava.util.List;/*** Simulated heap overflow *-xms20m minimum initial memory *-xmx20m Max Memory value *-xx:+heapdumponoutofmemoryerror*/ Public classDeapoutofmemory {/*** Inner class, used to create objects, stored in heap memory*/ Static classtestoom{} Public Static voidMain (string[] args) {//used to store mock objects, prevent GC recycling, and use list for object referenceslist<testoom> list =NewArraylist<>(); for (;;) {List.add (Newtestoom ()); } }}
Facilitates simulation of overflow, and memory snapshots when crawling memory overflows, such as setting virtual machine parameters:
-xms20m Java heap Memory initialization size is 20M
-xmx20m Java heap Memory maximum value is 20M
-xx:+heapdumponoutofmemoryerror Fetches a Java heap snapshot when an OutOfMemoryError exception occurs. where + is the meaning of the open.
Run the main method after setting the virtual machine memory parameters. In the case of a OutOfMemoryError exception, the Java Heap space is where it occurs.
Open the captured snapshot, as you can see that the number of internal class Testoom instance objects is 810326.
Java Heap Memory Overflow simulation