For the basic content of the JVM garbage collection mechanism, refer to the previous blog garbage collection mechanism (garbage Collection) Introduction
On a blog, the memory of the heap is divided into three parts: The young generation, the old age, the eternal life generation. This blog post will demonstrate how these three sections interact and actually demonstrate garbage collection.
1. First, all newly created objects are assigned to the young generation of Eden space, and two survior spaces are initially empty.
Represents the memory of a young generation running a real situation, the newly created object will be placed in Eden space, the "from" Survior space inside the number represents the current memory block experience garbage collection number. Without a garbage collection, the number of memory blocks is added one, and the larger the number, the longer it takes to exist.
2. When the Eden space is filled, a secondary garbage collection is triggered.
3. Triggering a secondary recovery, you need to clean up all the objects in Eden space, the objects that are not being used are all moved to the s0 surviving space, and the number of occurrences is 1. If the s0 surviving space cannot be loaded with all the surviving objects migrated from Eden, the extra objects will be migrated directly to the old age space. 3, 1 memory blocks are ignored here.
4. When the second garbage collection occurs, the same thing happens in Eden space, where no referenced objects are deleted, and the referenced objects are moved to the surviving area space. This time move to another surviving area space S1. If the S1 space is not sufficient to hold all the surviving objects from Eden, the remaining objects are moved directly to the old age.
S0, the last time the surviving space was saved, was also garbage collected, the age of the surviving objects was added 1, and was moved to the S1 survivor area. Similarly, if S1 cannot load all the surviving objects that have been moved, the remaining objects are moved directly to the old age. Empty the S0 space.
5. Next garbage collection, processing process, just the survival zone reversed, but also the referenced object was moved to S0 space. The surviving objects of age plus 1,eden and S1 space are emptied.
According to 4, 5 steps can be seen, no matter when the survival zone S0, S1, there is always an empty.
6. This step shows the object promotion. After some garbage collection, when some older objects reach a certain age valve (this example is 8), they are promoted from younger generations to the old age.
7. As the secondary garbage collection progresses, the object is continually promoted to the old age space.
8. The above covers almost all the process steps of the young generation. Eventually, the primary garbage collection is performed, and the old age space is cleaned up and compressed.
Resources
The generational garbage Collection Process, Java garbage Collection Basic, Oracle
Post
To observe memory usage for each generation of a Java process in the JVM, you can use the tool JVISUALVM. As follows. For a detailed demo click here (English).
Steps to [Java] garbage collection (Garbage Collection) Demo