1. Concepts
The heap is the most important area in JVM, the JVM specification states that all objects and arrays should be stored in the heap, in the execution of bytecode instructions, the object created will be deposited into the heap, the reference address of the object is deposited into the stack frame of the virtual machine stack. However, when the method is executed, the object just created will not be recycled immediately, that is, the object will not disappear with the stack frame, but will wait for the JVM background to perform GC before the object is recycled.
2. Specify heap size
-Xms: specifies the initial memory size of the heap, ms (memory start). Equivalent to -XX:InitialHeapSize;
-Xmx: Specifies the maximum memory size of the heap, mx (memory max). Equivalent to -XX:MaxHeapSize;
It is common to set -Xms and -Xmx to be the same so that the JVM does not need to modify the heap memory size after GC, improving efficiency. By default, -Xms is equal to physical memory size/64 and -Xmx is equal to physical memory size/4.
3. New and old generations
There are many garbage collection algorithms, but basically they divide the memory into two areas: the new generation and the old generation. The fresh generation holds newly created objects and the old generation holds objects that are still alive after many GCs (15 by default) have been performed.
You can configure the ratio of the old generation to the new generation by using the -XX:NewRatio parameter, the default is -XX:NewRatio=2, which means that the new generation accounts for 1, and the old generation accounts for 2. Generally, you don't need to adjust the ratio, and you only need to adjust the ratio of -XX:NewRatio if you know clearly that there are more or fewer objects that have been surviving for a longer period of time.
3.1 Cenozoic
The Cenozoic can be subdivided into Eden (Eden zone) and S0 and S1 zones.
Eden: Eden area, new objects are put in the Eden area first.
S0, S1 zones: The Survivor0 and Survivor1 zones, which can also be called from and to zones, are used to store objects that exist after MinorGC (YGC).
By default the ratio of Eden, S0, S1 is 8:1:1, which means that the Eden zone is 8/10th of the size of the new generation. this can be adjusted with -XX:SurvivorRatio.
3.2 Older years
The old age holds objects that are still alive after many GCs have been performed. By default, old ages occupy 2/3 of the memory area.
3.3 Animated Demonstration
Animation demonstrating the flow of objects through various regions of memory
- The object will be put into the Eden area first.
- After executing Young GC it will be put into S0 or S1 area, S0 and S1 can not be non-empty at the same time, the object will jump between S0 and S1 repeatedly.
- After a certain number of Young GCs (15 by default), assuming the object has not been recycled, it enters the old age region.
- If the size of the new object exceeds the size of the remaining space in the Eden area, it will go directly to S0 or S1, and if S0 or S1 can't fit, it will go directly to the old age.
- The old age continues to perform Old GC to reclaim the objects in it.
- Here Young GC and Old GC can also be called Minor GC and Major GC, they are not the names of the garbage collector, but just represent the garbage collection process of the young and old generations.。
4. Generational collection concept
The new generation old generation above is the Split Generation Collection Concept, some times it is called a Split Generation Collection Algorithm, but it is actually a concept. By default almost all garbage collection algorithms use the split generation collection philosophy.
Why does the garbage collection algorithm divide memory regions into new and old generations, and the new generation contains Eden, Survivor0, and Survivor1 regions?
This is because different objects live for different lengths of time, so different garbage collection algorithms should be adopted for objects with different lengths of time.
- If the objects in the new generation have a short survival time, then the "replication algorithm" (described in a later section) can be used.
- Objects in older generations have been alive for a long time, so it is not suitable to use the copy algorithm, you can use the "mark-erase algorithm" or "mark-organize algorithm" (described in later chapters).