. The storage structure and implementation principle of GC in net

Source: Internet
Author: User

GC (Garbage Collector, garbage collector) is a mechanism for automatically reclaiming memory, freeing up memory space for objects that are no longer in use.

In the. NET platform, our managed code is generally no longer concerned with memory management, and everything has the CLR (Common language Runtime) to help us finish. When we use the New keyword when we open up the memory space to create the object, the CLR allocates a chunk of memory to hold the object, and most of the time, we don't have to free up our own memory space, but the CLR frees us up at some appropriate time.

  Why GC?

1. Create new object to open up memory space, need to free memory after use, improve performance

2. Prevent developers from directly manipulating memory and improving security

  The process of GC

We run. NET program, OS Loader first recognizes the IL, then loads the CLR's core library, performs a series of necessary processing, and the CLR comes to the code entrance that we write.

When we create a class using the new operator in code, the CLR allocates a piece of memory on the memory area known as the GC heap (GC heap) to hold our objects, and if the object's size exceeds 85K bytes, the object is created in the Loh (Large object) for performance reasons. Heap) Instead of " Note 1" on the GC heap, if we define a destructor in class to release the unmanaged resource " Note 2", the CLR will be in a queue called finalizer (Finalizer) Add a pointer to the class.

Our program in the process of running, at some point need to garbage collection " Note 3", the GC will temporarily suspend all threads, and then determine the object reference roots " Note 4", And according to the reference relationship to create an object from the roots can be achieved by the object graph, these objects are still in use, and those who have been created but not in the object graph object is not attainable, that is, garbage, belongs to the object to be recycled. The still-used objects are then moved to the longer-lived area " Note 5," which changes the area pointer to reclaim the object, compresses the memory to remove the memory gap, and repairs the reference pointer to the moving, still-surviving object, which is not recycled on the first collection for objects that have destructors. Instead, it is removed in the finalizer queue and added to another list of objects that are labeled ready to terminate, another GC thread calls the Finalize () of the object that this list points to, reclaims the unmanaged resource, and then removes the item from the list, and the next GC actually reclaims the object.

  Note 1: Details of objects created on the heap

1) For more efficient GC,. NET divides the GC heap into 3 generations, gen0,gen1 and Gen2.

2) These 3 generations are only logically divided, in memory, their addresses are contiguous.

3) The size of the sum of Gen0 and GEN1 is approximately 16M (Workstation GC mode) and 64M (in Server GC mode).

4) The newly created object size is less than 85k on Gen0, and greater than 85K is created on the Loh.

  Note 2: Define a destructor to release an unmanaged resource

The Finalize method is used to dispose of the unmanaged resources used in the object, which is a security precaution for the Dispose () method, that is, when a call to Dispose () is not displayed in the code to release an unmanaged resource, the GC calls the Finalize method to release it. The Finalize method does not directly release the unmanaged resource, but instead calls Dispose (false) to release it. Since. NET2.0, C # cannot directly override the Finalize method, which is implemented by a destructor, which in IL is interpreted as:

protected Override voidFinalize (){     Try    {//perform a custom resource cleanup operation    }finally    {Base. Finalize ();     }}

By default, a class does not have a destructor, and its finalize () method is not called when the GC is in progress.

  Note 3:GC The timing of the occurrence

1) When the memory usage of Gen0 reaches a threshold, the Gen0 GC will be raised, and the same Gen1 will be Gen0 and GEN1 while the GC is reached, and the full GC will be raised if the Gen2 is reached.

2) When Windows reports low memory

3) when calling Gc.collect

4) Other scenarios: CLR unload appdimain, insufficient physical memory, etc.

Note 4: Determine the object reference root

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

  5:GC the transfer of objects at the time of injection

1) Gen0 GC, the Gen0 of the surviving objects will be moved to GEN1, and then compress the GEN1, so that the memory in Gen1 continuous, the same Gen1 move to Gen2.

2) When Gen2 GC, the GC that occurs at this time is also called the full GC, which reclaims objects on the entire heap, and the objects on the Gen2 no longer move, but instead compress the memory space.

3) The objects in the Loh are reclaimed at full GC, but their memory is not compressed, but instead use a free list to record the free space in the Loh and manage the freed space.

4) If the object is pinned objects, this object cannot be moved and will cause 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.