Mark and compressAlgorithm
When the applicationProgramAt startup, a certain number of addresses are allocated to the application. These address spaces are managed heap management heaps used to store object indexes in the application. If you want to add a new object and the Application Management heap does not have enough space, the garbage collection process will be executed.
If the original object is out of the range, the space will be released and a new object will be created. However, if the Garbage Collector cannot release the space, the application will still create the object with an error.
Example: At the beginning of the application, Za has created several objects A, B, C, and D, whose indexes are placed in the management heap.
Create an object e in the application lifecycle. Because the management heap does not have enough space to allocate, garbage collection is required. After the last collection, objects A and C are out of the range.
At the beginning of garbage collection, a list of all application objects within the scope is generated. This list includes object B and Object D. The algorithm gradually retrieves the management heap to find objects that are not currently in the list. Once an object out of the range is found, these objects are marked as inaccessible, and the Management heap is compressed. Then place the index of the new object e into the heap.
Generation Concept
In order to improve the efficiency of retrieval heap during garbage collection, this paper proposes the concept of generation. The longer the object is, the less likely it is to be out of range in the heap. Therefore, first retrieve new objects. If you still need space to detect objects that take a long time, this will improve efficiency.
. Net garbage collection supports three generations: generation 0, Generation 1, and generation 2.
Generation 0: the object is the object created during the previous garbage collection;
Generation 1: objects within the scope after a garbage collection;
Generation 2: objects within the range after repeated garbage collection.
During garbage collection, if the space after the 0-on-generation object is not enough to be allocated to the new object, the garbage collector will move to generation 1. If not, the garbage collector will move to generation 2.