Java's automatic memory management system mainly includes two aspects:
- Automatically allocates memory to objects.
- Automatically recovered to the memory allocated to the object.
This article also deployed around the two points
A. Memory Allocation Rules
1. Eden preferentially partitioned area
In most cases, JVM will be in the Eden
priority allocation target area, if Eden
there is not enough space, then once Minor GC
. By parameter -XX:+PrintGCDetails
allows the virtual machine to print the log during garbage time, to help us see the memory usage before and after recovery.
Example: If the memory size is now specified as follows:
- New Generation -> 10M
- Eden District -> 8M
- from area -> 1M
- to District -> 1M
- Years old -> 10M
Then we have to create four objects in your code:
byte[] byte1 = new byte[2MB];
byte[] byte2 = new byte[2MB];
byte[] byte3 = new byte[2MB];
byte[] byte4 = new byte[4MB];
复制代码
When After creating the third object, Eden District has spent 6M of space to store byte1, byte2, byte3 three objects, and then create a fourth target, Eden area has been coupled with a fit from the area, such as previously, this time will trigger once MinorGC
, to transfer three 2MB objects to the old era, freeing up space for the Eden area of the fourth object.
So, after the execution memory as follows:
- New Generation -> 10M
- Eden area -> 8M (remaining 4M) stored byte4
- from area -> 1M
- to District -> 1M
- Years old -> 10M (remaining 4M) storage byte1, byte2, byte3.
2. Large objects directly into the old year
By -XX:PretentureSizeThreshold
greater than the value of the parameter object directly assigned to the old era.
3. Long-term survival of the object enters years old
How considered long-term survival?
JVM for each object defines a 对象年龄计数器
. When the object is initially assigned to the new generation Eden area, and after a MniorGC
post is still alive, and Survivor areas able to accommodate it, the object is transferred to the Survivor
region, age becomes one.
In the Survivor
target area did not get through the first MniorGC
time, up to the age of 1, reached the age when we set the age threshold (JVM default setting 15), it will enter the old era. 15-year-old has entered old age ....
age threshold parameters can -XX:MaxTenuringThreshold = 指定值
be set.
To determine the age of objects optimization
JVM, if Survivor areas of the same age, the size of all objects greater than the sum Survivor space half, then these students who go directly to the old era. No need to wait until the age threshold above.
II. Space allocation and recovery strategies guarantee mechanism (Mnior GC or Full GC)
First of all introduce Mnior GC
and Full GC
differences:
MniorGC: place in the new generation garbage collection activity, due to the short-lived new generation of Java object properties, such activity is frequent garbage collection, recovery faster. (Just sweep the shreds of paper) Full GC: garbage collection events occur in the elderly, but the emergence of a Full GC, will be accompanied by a MniorGC, because the old era objects are basically large objects, long-lived, so the Full GC Mnior GC slower than the speed of more than 10 times. (Like moving large stones)
Garbage collection algorithm uses a new generation of replication algorithm , when once Mnior GC
when will the new generation of active area ( Eden区
and Survivor are From区
copied) object to the survival of Survivor to区
, these objects if the memory area is not sufficient to lay down , you then need to go into action years old, allocation guarantee mechanism, the object does not fit into the old era.
So, during the Monior GC
former, JVM will do check the following procedure to confirm whether old's able to let go of those objects to be selected Mnior GC
or Full GC
.
Reference:
in-depth understanding of the Java Virtual Machine