Garbage collection and garbage collection
1. the CLR Garbage Collector uses the generation mechanism. Currently, it supports three generations: 0, 1, and 2.
- The newly constructed object added to the heap is called The 0th generation.
- After 0th generations of garbage collection, 0th generations of survivors were promoted to 1st generations.
- After the garbage collection of 1st generations, survivors of the first generation were promoted to 2nd generations.
During CLR initialization, the budget is selected for each generation. The budget for the 0th generation is about 256 K, the budget for the 1st generation is about 2 M, and the budget for the 2nd generation is about 10 M. In practice, the garbage collector uses a heuristic algorithm to adjust the budget of each generation.
2. when the software starts to run, it reserves a continuous memory for each Generation (this is not strict, but does not affect the description of this problem ), at the same time, a pointer P pointing to the unused part of the memory area is maintained. When you need to allocate space for the object, the address where P is directly returned, and adjust P accordingly. 3 .. NET will divide the object into two different objects, one is the object smaller than 85,000 bytes, called a small object, it corresponds to the general situation described above; the other is an object with a size above 85,000, which is called a large object. In. NET, all Large objects are allocated in another special continuous memory (LOH, Large Object Heap), and each Large Object belongs to G2 at creation, that is to say, LOH is processed only when the garbage collection of Generation 2 is performed. In addition, the memory will not be compressed during LOH garbage collection! Furthermore, the LOH space usage is also very special-when a large object is allocated, the runtime will first try to allocate it at the end of the LOH. If the space at the end is insufficient, the system will attempt to request more memory space from the operating system. Only when this step fails will the system re-search the memory space left by the previous invalid object. after an object is marked as spam, its Finalize method is automatically called, provided that the object overwrites the Finalize method of the object.