Garbage collection GC:. NET self-active memory management on (ii) memory algorithms

Source: Internet
Author: User

Garbage collection GC:. NET self-active memory management on (ii) memory algorithms
    1. Garbage collection GC:. NET self-active memory management on (a) memory allocation
    2. Garbage collection GC:. NET self-active memory management on (ii) memory algorithms

    3. Garbage collection GC:. NET self-active memory management on (iii) Terminator

Objective

. NET has completely conquered the embarrassment of developers tracking memory usage and controlling the release of memory. However, you may want to understand how the GC works in the afternoon. This series of articles will explain how memory resources are properly distributed and managed, and include a very specific description of the intrinsic algorithm. At the same time. It also discusses the GC's memory cleanup process and what to clean up and how to force cleanup.



Memory algorithms

GC detection is used to see if an object in the heap is no longer being used by the program. Assuming this object exists, the memory used by these objects can be recycled.

(If there is no free memory space in the heap, the new operator throws a OutOfMemoryException exception) how does the GC know if an object is still being used by the program? Can you imagine. This is not an easy question to answer.

Each program has a set of root nodes (roots). They are used to identify the storage space to locate objects in the managed heap or to point to empty (null) objects. For example, all global object pointers or static object pointers in a program are considered part of the program root node (roots).

In addition, the line stacks is considered part of the program root node (roots), regardless of the local variable or the parameter object pointer.

At last. All CPU registers that include the managed heap object pointers are also considered part of the program root. This set of activations is based on the JIT compiler and the CLR maintenance, and can be interviewed by the GC's algorithmic system.

When the GC is executed. It will if all the objects in the heap are garbage. In other words, it does not matter if the program root node starts with the object in the heap. Now. The GC starts by looking at the program root node and creating a map for all objects associated with the program root node. For example, a GC might locate a global variable that points to any object in the heap.



Depicts a heap with several objects, and the program root node points directly to the object a,c,d,f. All of these objects become part of the map. When Object D is added, the GC detects that it also points to the object H. So object h is also added to the map. The GC will always be so recursive to all associated objects.


Managed heap (the ability to refer to heap and stack doubts: Diagram of C # heap and Stack)) is allocated on the object:


Once the GC has completed this part of the mapping, the GC will then check the next root node and then recursively return the associated object. The last complete mapping. One difference, however, is that the GC will stop until the current node is no longer stretched until it has been mapped before a recursive object is found. But the other nodes will continue.

Other nodes assume that the same situation will stop until all objects have been mapped. This mapping method has two purposes. First. Avoiding repeated mapping of one or a group of objects greatly improves program performance. Second. It avoids mapping a dead loop.



When all of the root nodes are checked and mapped, the GC map will include all the program root nodes (direct or indirect access) to the object; Suppose an object is not in this map. The Description program root node will never be able to access it. Then this object is thought to be Garbage

Now the GC is able to access the heap in a straight line, looking for contiguous chunks of memory that the garbage object occupies. The GC then moves the memory space occupied by the non-garbage object to the block of memory occupied by the garbage object (the standard memcpy operation). and remove the memory gap in all heaps (the gap between the blocks of memory the object occupies). Of course, the movement of this block of memory affects all associated memory pointers, as memory addresses change. Therefore, the GC must alter the program root node (roots) and ensure that all the affected pointers point to the new address of the object. Other than that. Assume that no matter what object includes a pointer to another object. The GC will also be responsible for correcting these pointers at the same time. I have also been able to participate in one of the articles:

in-depth illustration of C # heap and Stack C # heap (ing) VS stack (ing) section Sixth understand garbage collection GC, get the program performance。

Is the managed heap after the garbage collection is run:


Managed heap After GC is recycled:



After all the garbage objects have been reclaimed. All non-spam objects become compact again, and non-spam pointers are all fixed. Nextobjptr will be placed after the last non-spam object. At this point, the new operator starts to try to create the object, and the resource requested by the program is created successfully.


As you can see, the GC produces a noticeable performance drain, which is a major drawback of using GC. However, remember that the GC is recycled only when the heap is full, before recycling. The managed heap is significantly faster than the heap when the C language executes. The GC also provides some optimizations that can significantly improve the performance of garbage collection. Perhaps the article will discuss how GC optimizes garbage collection.


There are some very important points to be pointed out. You no longer need to write code to manage the lifetime of your program resources. The two bugs mentioned at the beginning of the article will no longer exist. First of all. It is no longer possible to generate resource leaks, as resources (i.e., garbage) that are not visited by the program root node (roots) are recycled. Second, you may no longer be able to access a freed resource, and it will never be released because it is assumed that resources can be interviewed. Suppose the resource is not accessible. There is no reason for us to visit it.

The following code shows how resources are assigned and managed:

Class Application {public    static int Main (string[] args) {      //Create ArrayList object in the heap, MyArray now as the program root node      ArrayList MyArray = new ArrayList ();      Create 10,000 objects in the heap for      (int x = 0, x < 10000, x + +) {         Myarray.add (new Object ());          }      Today, MyArray is a root (online Cheng).      //So. MyArray and 10,000 objects are accessible            Console.WriteLine (myarray.length);      After myArray the last reference in the Code (Console.WriteLine (Myarray.length)), MyArray is no longer a root      //Do not have to wait until this method returns. The JIT compiler will know that after the last reference to myarray it is identified as a non-root node            //Because myarray is no longer the root node, all 10,001 objects are no longer available//They are considered to be      garbage      // But they'll always be there until the GC recycles them   }}


Assuming the GC is so good, why doesn't C + + use it? The reason is that the GC must be able to identify the program root node (roots), and all object pointers must be found. C + + agrees that pointers are type-converted, so it is not possible to determine what a pointer is pointing to.

In the CLR, in general language execution, the managed heap can always determine the exact type of the object, and the metadata metadata information determines which other objects the members of an object point to.


Summarize
This article describes the algorithm for GC garbage collection in the. NET framework, and simply mentions the differences from C and C + +.

Understanding the memory algorithm lets you know why the GC is fast. What the GC does consumes performance, allowing you to have a clear idea of the performance of your own programs.

In the next article I will continue to introduce the garbage collection GC's own active memory management: the finalization of the end-node device.






translation: Http://msdn.microsoft.com/en-us/magazine/bb985010.aspx

Garbage collection GC:. NET self-active memory management on (ii) memory algorithms

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.