GC note in. NET

Source: Internet
Author: User
GC (Garbage Collector) is a mechanism for automatic memory recovery, releasing the memory space of objects that are no longer in use.

On the. NET platform, our managed code generally does not care about memory management, and everything has the CLR (Common language Runtime) to help us. When we use the new keyword to open up the memory space for object creation, the CLR will allocate a piece of memory to store objects. In most cases, we do not need to release the memory space ourselves, instead, the CLR can help us release it at an appropriate time.

  Why GC?

1. Create a new object to open up memory space. after use, the memory needs to be released to improve performance.

2. Avoid Direct Memory operations by developers to improve security

  GC Process

After we run the. NET program, the OS Loader first identifies the IL, then loads the core CLR library, and performs a series of necessary processing. Then, the CLR comes to the entrance to the code we compile for execution.

When we use the new operator to create a class in the code, CLR allocates a block of memory in the memory area called GC Heap to store our objects, if the Object Size exceeds kb, the Object is created on the LOH (Large Object Heap) instead of the GC Heap for performance reasons 【Note 1]. If we define the destructor in class to release unmanaged resources 【Note 2], The CLR will add an entry pointing to this class in a place called the Finalizer Queue.

During the running process of our program, garbage collection is required at some time 【Note 3], GC will temporarily suspend all threads, and then determine 【Note 4And create an object graph that can be achieved by using the reference relationships. These objects are still in use, the created objects that are not in the object graph cannot be reached, that is, garbage, and belong to the objects to be recycled. Then, move the objects that are still in use to the region with a longer retention period 【Note 5], Change the region pointer to recycle objects, compress the memory to remove memory gaps, and fix reference pointers to moving surviving objects. For objects with destructor, the object will not be recycled for the first time, but will be removed from the terminator queue and added to another list marked as the object to be terminated, another GC thread will call the Finalize () of the object to which the List points, reclaim the unmanaged resources, and remove the items from the list. The next GC will actually recycle the object.

 

  Note 1: Details of object creation on Heap

1) for more efficient GC,. NET divides the GC heap into three generations, Gen0, Gen1 and Gen2.

2) These three generations are only logically divided. In the memory, their addresses are continuous.

3) the sum of Gen0 and Gen1 is about 16 M (in workstation GC mode) and 64 M (in server GC mode ).

4) the newly created object with a Size smaller than 85k is located on Gen0, and a Size greater than 85K is created on LOH.

  NOTE 2: Define destructor to release unmanaged Resources

The Finalize method is used to release the unmanaged resources used in the object. It is a security protection measure of the Dispose () method, that is, the call of Dispose () that is not displayed in the code () to release unmanaged resources, the Finalize method is called to release the GC. The Finalize method does not directly release the unmanaged resources, but call Dispose (false) to release the resources. Starting from. NET2.0, the override Finalize method in C # cannot be implemented through the destructor. The Destructor will be interpreted:

Protected override void Finalize ()
{
Try
{
// Clear Custom Resources
}
Finally
{
Base. Finalize ();
}
}

By default, a class has no destructor, so the Finalize () method is not called during GC.

  NOTE 3: GC occurrence time

1) when the memory usage of Gen0 reaches a threshold, Gen0 GC will be triggered. Similarly, when Gen1 reaches, Gen0 and Gen1 will be GC at the same time. If Gen2 reaches, Full GC will be triggered.

2) When Windows reports insufficient memory

3) When GC. Collect is called

4) Other cases: CLR detaching AppDimain, insufficient physical memory, etc.

Note 4: determine the object reference Root

The reference root of an object mainly comes from: FInalize Queue, object pointer in the CPU register, global object, static variable, local object, function call parameter, etc.

  Note 5: object transfer during GC

1) During Gen0 GC, the surviving objects in Gen0 will be moved to Gen1 as a whole, and then Gen1 will be compressed to make the memory in Gen1 continuous. Similarly, Gen1 will be moved to gen2.

 

2) When Gen2 GC occurs, GC is also called Full GC, which recycles the objects on Heap. The objects on Gen2 are no longer moved, but compressed memory space.

3) objects in LOH are recycled during Full GC, but their memory is not compressed. Instead, an idle list free list is used to record the free space in LOH and manage the released space.

4) if the object is a pinned object, the object cannot be moved, causing memory fragmentation.

  

 

 

  

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.