Section I. Garbage collection Algorithms and complete collections (full GC)
The garbage collector tracks all referenced objects, collates objects that are no longer referenced, reclaims the corresponding memory, and uses the "Mark and Purge" algorithm to recycle objects in two steps:
Step 1.mark-sweep: From the application root, using the cross-reference relationship, traversing its dynamically allocated on the heap of all objects, indicating that the objects need to be recycled, mark the surviving objects, to be marked.
Step 2.Compact: The memory of the surviving objects to move, modify their pointers, so that the continuous in memory, so that the free memory is continuous, that is, complete the memory release work, but also solve the memory fragmentation problem, this process can also become the compression of the pointer.
The garbage collector typically divides objects in the managed heap into 3 generations, this can be learned by calling Gc.maxgeneration, the object according to the length of time to be divided into generations, the shortest in the No. 0 generation, the longest in the 2nd generation, the 2nd generation of objects tend to be relatively large, the second generation of space is called large Object Heap, for 2 of the objects of the collection, and the No. 0, 1 generation of the most important difference is that there is no pointer movement compression process.
Figure 1 Collection of objects
As shown in the figure above, the area on the left is the first GC-time structure, note that the GC tag is for those surviving objects, not the ones that need to be recycled, so for the first time, objects B and D are not tagged, so they are recycled, and then the GC moves the object memory pointer to make the space contiguous.
Next look at the middle part, the second GC begins, the C object is not marked, so it is recycled, the next A, D, f Three objects are compressed, forming a continuous memory space, and formed the 1th, 2, 3 generation region.
Next look at the rightmost part, the D object is not marked, because the D object is in the 2nd generation, so after the D object is recycled, the GC does not start the compression step because the resource is expensive to move the pointer to a large object.
For a 2nd-generation GC, called a full GC, the newly allocated objects in the No. 0 generation (the maximum length of the 0 generation space is usually 256K), are assigned by address order, they are usually some local variables, and the 1th generation (the maximum length of 1 generation space is usually 2 MB) is an object that still resides in memory after 0 generations of garbage collection. They are usually objects such as forms, buttons, and 2nd generations are objects that still reside in memory after several times of garbage collection, usually some application objects.
It is possible to see a full GC with the most resources required, possibly for a few seconds or more than 10 seconds.
The memory allocations for the managed heap are in segments (Segment), which typically create 2 segments for GC Heap when the CLR starts, to store the No. 0, 1, and 2nd generation objects, and the following are the GC heap cases that are looked up through the WinDbg tool:
Figure 2 WINDBG view of GC Heap
As can be seen, the GC heap is divided into two segments, three generations, each generation of the starting address decimal difference value of 12.
In understanding, it is important to note that GC reclaims the reference type in the program, the value type is stored in the stack, and the duty type object automatically releases the memory----that is, the stack, and does not require garbage collector management.