The allocation and release of memory in java and C# are automatically managed by virtual machines. I have previously introduced the GC object recycling method in CLR, which is a generation-based memory recycling strategy. In fact, in java, the JVM object recycling strategy is also based on the idea of generational division. The purpose of this is to increase the garbage
The performance of recycling avoids the delay in the program response caused by checking all objects in the heap, because when jvm executes GC, it stops the word, that is, terminates the operation of other threads, and only restores the operations of other threads after the recycling is completed. The idea based on generational division is: jvm only needs a small part of the memory every time he executes the garbage collector.
Object references are checked, and this small number of objects have a shorter life cycle, which speeds up the performance of garbage collection. Below we will introduce the basic strategies of generation-based memory recovery algorithms in Java in the future:
1. Generation division of jvm heap memory
In a generation-based memory recovery strategy, the heap space is usually divided into 3 generations, young generation, old generation (or tenured generation), and permanent generation. Among the young generation, there are three small areas: Eden, S0, and S1, as shown in the figure below:
Among them, the new objects are always assigned to the age generation. When the space of the young generation is filled, a garbage collection needs to be performed, that is, minor GC is executed, to recycle the objects that are no longer referenced, and at the same time increase the age of the surviving objects. The surviving objects in the age generation have an age identification field. Once they reach a certain threshold, the still surviving objects will be promoted to the old generation space.
The space of the old generation is used to store objects that have survived for a long time, that is, objects with a long life cycle. Once the surviving objects in the young generation space reach a certain age threshold, it will be automatically promoted to the old generation. When the space of the old generation is filled with objects, Major GC will be executed once. Compared with minor GC, the number of executions of Major GC is much less than that of minor GC. At the same time, Major Gc executes longer than Minor Gc. Because it involves more object scanning. This generational idea is also based on the better choice in practice that in practice, the newly allocated objects have a shorter life cycle and the elderly objects have a longer life cycle.
At the same time, when Minor Gc and Major Gc perform garbage collection, they take stop the world event, that is, terminate the running threads and restore all threads when the GC is executed.
For permanent generation memory, it is mainly used to store relevant information of metadata, information of classes and methods. When a class is no longer in use, it will be recycled. When Full GC is executed, permanent generation memory will be scanned for garbage collection.
2. Processing process of generation-based garbage collection
First, at the beginning, the new object is assigned to the Eden area, s0 and s1 are empty. When the space in Eden is filled, perform Minor GC once. The garbage collector will move the referenced object to the s0 area, and the objects that are no longer referenced will be deleted. At the same time, the age of the surviving object is 1. After GC is identified, the Eden and S1 areas are empty as shown in the figure below.
The next time Minor GC is executed, the same as the previous execution steps, the only difference is that the referenced object, that is, the surviving object, will be moved to the S1 area, and the age of the object surviving in the s0 area will increase by 1 and become 2, as shown in the figure below.
When Minor GC is executed again, the surviving object will be moved to S0 area and add 1 to the age of the surviving object as shown in the figure below
When the Minor GC is finally executed, it is found that the surviving object in S1 reaches 8 (assuming the threshold is set to 8), and the object will be promoted to the old age memory, as shown in the figure below.
Z
When the old heap space is filled with objects, Major Gc will be executed once, which will clear objects that the old heap is no longer referenced, and at the same time, compress the space. As shown in the figure below.
The above brief discussion on the garbage recycling strategy in jvm is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.