Overview
Unlike C + +, we don't have to worry about the release of object space in Java, which is largely due to the garbage handlers in Java (GC) that help us automate the release of object space.
Here we take a few questions to learn:
- How does heap memory be divided into generations?
- How do the different generations work together?
1. How does the heap memory be divided into generations?
Use a picture to describe (the area size does not represent the actual amount of space occupied)
Heap memory is divided into: younger generation (young) + older generation (old), and younger generations:Eden Zone + Survivor District * 2.
Usually the ratio of each region in the younger generation is:eden zone: Survivor0:survivor1 = 8:1:1
To make the object do not enter the old area as much as possible.
S0 and S1 are dynamically automatically resized by default, and you can use '-xx:useadaptivesizepolicy ' to turn off dynamic adjustments
2. How do the different generations work together?
To describe this problem as a copy of an object between each region, we assume that the object has survived.
When we write a code like this,
New User ();
The user object is first placed in the Eden area, where the first minor GC occurs when the Eden is full, and the garbage collector randomly selects a zone in S0 and S1 (assuming S0 is selected) to hold the remaining surviving objects in the Eden area. The second minor GC occurs when the Eden Zone is full again, when the garbage collector copies the surviving objects in the Eden area + S0 to S1, and when the Eden Zone is full again, a third minor GC occurs, and the garbage collector will keep the Eden area alive Objects + The surviving objects in the S1 are copied to the S0, so they are duplicated.
So, under what circumstances will the object enter the old area?
- When the Eden Zone is full, when an object is about to enter S0 or S1, the object is copied to the old area if S0 or S1 does not hold an object in the Eden area.
- If an object undergoes a set number of copies in S0 and S1, it is copied to the old area.
By the way: The Young generation garbage collection uses the copy algorithm, the old age recycling uses the mark clears the algorithm.
If the description is not accurate, trouble to specify, thank you!
Talking about the memory generation and recovery of Java heap