The. Net Discovery Series three deep understanding. NET garbage collection mechanism (on)

Source: Internet
Author: User
Tags compact garbage collection memory usage

Objective:

Composition. NET platform is a very important part----garbage collector (garbage Collection), let's talk about it today. Think about it without GC,. NET can also be called a platform? Although all languages are compiled into MSIL, but the operation of the resource recovery work is "separate", which not only increases the difficulty of programming, but also makes the memory management work becomes very complex (different language processing memory small differences, will be magnified in the recovery of resources), but also not conducive to platform transplant.

This article will provide a comprehensive overview of how. Net garbage collection runs, algorithms, and key methods associated with garbage collection.

Speaking of the garbage collection mechanism, few people know that garbage collection is not accompanied by Java, as early as in 1958, Turing Prize winner John invented the Lisp language has provided the function of GC, this is the first appearance of GC, is a flash of thought! Then, 1984 Dave Ungar invented the small talk language for the first time formally adopted the GC mechanism.

. NET garbage collection mechanism is a big topic, if you do not have access to a language like C + +, it is difficult to understand how GC is an important and exciting thing:

1. Improve the cohesion of the software system.

2. Reduce the complexity of programming so that programmers do not have to distract themselves from dealing with the destructor.

3. Does not prevent the designer from System abstraction.

4. Reduce the bug caused by improper memory usage.

5. The successful management of memory from the time the program is written, away from the runtime, so that unpredictable administrative vulnerabilities into predictable.

Body:

In this paper, the "GC algorithm and working mode", "GC Key Method Analysis" two sections. NET garbage collection mechanism.

The first section. The algorithm and working mode of GC

1. Algorithm

The nature of the garbage collector is to track all referenced objects, organize objects that are no longer referenced by objects, and reclaim the corresponding memory.

This sounds similar to an algorithm called reference count (Reference counting), but this algorithm needs to traverse all objects and maintain their references, so it is less efficient and can easily cause memory leaks when a "ring reference" occurs. So. NET uses a method called Mark and Purge (Mark Sweep) to accomplish the above task.

The "Mark and Purge" algorithm, as the name suggests, has two capabilities:

"Mark" ability--the identification of garbage: Starting from the application root, using the mutual reference relationship, traversing all the objects that are dynamically allocated on the heap, the object that is not referenced is not marked, that is, it becomes garbage; The surviving object is labeled, which is maintained as a "root-object-accessible graph".

In fact, the CLR will be the object of the relationship as a "tree graph", no doubt, understand the data structure of the students know that with the "tree graph" concept, will accelerate the speed of traversal objects.

Detection, labeling object references, is a very interesting thing, there are many ways to do, but only one is efficient,. NET is to use the stack to complete, in the continuous stack and out of the stack to complete detection: First in the tree map to select a need to detect the object, the object of all references to the stack, so repeatedly until the stack to empty until. The stack's empty meaning has traversed all the objects that the local root (or node in the tree diagram) can reach. The Tree graph node scope includes local variables (in fact, local variables are quickly recycled because their scopes are obvious, well controlled), registers, static variables, and these elements are repeated. Once finished, the memory is examined individually, and objects without tags become garbage.

"Purge" capability-reclaim Memory: Enable compact algorithm, move in-memory surviving objects, modify their pointers to keep them in memory, so that the free memory is contiguous, which solves the memory fragmentation problem when allocating memory to new objects again The CLR does not have to look for memory space in the fragmented memory for the new object, so the allocation speed is greatly increased. But with the exception of the large object (large Object heap), the GC does not move an in-memory jumbo because it knows that the current CPU is not cheap. Usually, large objects have a long lifetime when a large object is in the. NET managed heap, it is allocated to a particular part of the heap, and the overhead of moving a large object exceeds the performance that can be improved by defragmenting this portion of the heap.

In addition to the compact algorithm will increase the speed of allocating memory again, if the newly allocated objects are in a compact position in the heap, the performance of the cache will be improved because the objects allocated together are often used together (the local principle of the program), so it is important to provide a continuous blank memory space for the program.

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.