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

Source: Internet
Author: User

Garbage collection GC:. NET self-active memory management on (a) memory allocation
    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. 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, the GC's memory cleanup process and what to clean up will also be discussed. How to force cleanup.


Introduction

Implementing reasonable resource management for your application is a difficult and tedious task. This may divert your attention from the actual problem you are currently solving. So, suppose there is an existing mechanism for developers to manage an obnoxious memory management. Would it be a matter of pleasure? The answer is yes!. In. Net. There is a garbage collection mechanism called GC.


Each program needs to use some computer resources, such as memory, graphics card. networks, databases, and so on. In fact, in an object-oriented environment, each type represents a resource that the program needs to use.

Assume that you want to use these resources. You need to allocate memory to render this type.

Here are the steps to access these resources:

    1. Allocates memory to a type resource.

    2. Initialize memory and type resources and make resources available.


    3. Use these resources to access type instance member information (repeat on demand).
    4. Destroying and cleaning up resources
    5. Freeing memory

This looks very easy. But it is a fundamental source of procedural errors. How many times did the ape forget to release idle memory? How many times has the ape tried to access the memory that has been freed?

These two kinds of bugs are the worst cases, because they lead to abnormal results and the occurrence time is not pre-measured. For other bugs, when you see a program execution error. Just fix it. The easiest of these two bugs causes program resource leaks (wasted memory) and program objects to crash (unstable). It also causes the application to produce unpredictable behavior at unpredictable times. Of course. There are a number of tools that can be used to track bugs such as monitoring.

When we test the GC, you should know that it completely overcomes the problem of developers tracking memory usage and determining when to release memory. However, garbage collection GC does not understand anything about the type of resources represented in memory.

This means that the GC does not know and will not run the fourth step: Destroy and clean up resources. In the. NET Framework, program apes write code about destroying cleanup resources in method Close,dispose,finalize, which may be described in the article. It's just that. The GC can decide what to call these methods on its own initiative.

Some types of resources do not need to be cleaned up.

For example, the rectangle type can be completely cleaned up by destroying its left,right,width and height in memory. On the other hand, a file type resource or network connection type resource needs to be clearly cleaned up to destroy the code. I will explain how to complete these tasks properly.

Now, let's look at how memory is allocated and how the resources are initialized.


Memory allocation

The. NET CLR allocates all resources to the managed heap. It's kind of like a heap in C, but you don't have to release resources. Because idle resources will be released on their own initiative in. Net. Now there is a problem. How does the managed heap know when an object will no longer be used by the program? I'll take a brief look.
Today there are a lot of GC algorithms. Each algorithm is tuned for a particular environment to achieve the best performance. This article focuses on the GC algorithm used by the. NET CLR. Let's start with the basic concepts.


When a thread is initialized, execution will be scheduled for an unused contiguous address space. This address space is the managed heap ( C # heap and Stack C # heaps(ing) VS stack (ing). A pointer is maintained at the same time in the heap, and we call it the next object pointer.

This pointer tells us where the next program object will be allocated to the heap. At the beginning of the program, this pointer is set to the most basic memory address (which can be understood as the first position).

The program uses Newkeyword to create a new object. This operation first needs to determine if the intended address space is sufficient to store the new object (enough memory space). Assumptions enough. Nextobjptr (the next object pointer) points to this object in the heap. The object constructor is called. Finally, the object memory address is returned.
managed Heap (a friend who is puzzled by heaps and stacks can refer to the C # heap and stack in a comprehensible diagram):
(nextobjptr: Next object pointer)


At this point, Nextobjptr skips this object and points to the memory address of the next object that will be deposited. For example, there are three objects in the managed heap: A,b,c. The next object will be placed at the address pointed to by Nextobjptr (immediately after C).

Now let's see how the heap of C language allocates memory.

In the C language heap. Allocating memory for an object requires a linked list of data structures. Once a larger block is found, the cutting block is made. Then the pointer in the list node needs to be adjusted to ensure that all data is intact (C language is not ripe, original: In a c-runtime heap, allocating memory for an object requires walking though a linked List of data structures. Once a large enough block is found, that block have to being split, and pointers in the linked list nodes must are modified to Keep everything intact. )。 For the managed heap in. Net. Object assignment is simple. You simply need to add a value to the pointer. This is very fast compared to that. It turns out that allocating an object in the managed heap is almost as fast as allocating memory online Cheng!

Up to now, it sounds as if the managed heap is far superior to the C language heap in speed and simplicity of implementation. But. Having these advantages for the managed heap requires a major premise: address space and storage space are infinitely large. Of course, this is somewhat unrealistic, but the managed heap must use some mechanism to make this so-called if it is set up. This mechanism is garbage collection GC. Let's see how it works.

When a program uses the new operator to create an object, there may not be enough address space to place it. To check if the address space is sufficient, the managed heap will try to place the object in the Nextobjptr position, assuming that the nextobjptr is moved beyond the address space boundary. That means the heap is full and the GC is garbage collected.

In fact, the GC will be garbage collected when the No. 0 generation (perhaps the generation in the GC) is full. In simple terms. The generation in GC is a mechanism implemented by GC to improve program performance. The principle is that the newly created object belongs to the younger generation of GC, and the objects created earlier in the application life cycle are older generations. Dividing the objects into different generations allows the GC to know the specific generation to be garbage collected, rather than reclaiming the entire managed heap.


Summarize

This article is intended to give you a preliminary understanding of garbage collection GC and memory allocation. I have to say that understanding memory allocation is very important for a program ape. Let's say you want to write high-performance code. Although we do not have to allocate memory in the same way as the C language, there is a little bit of discrimination in memory allocation that is somewhat discriminatory (just a little bit). All right. No offense whatsoever, please don't misunderstand).

The next article will continue to cover the garbage collection GC's own active memory management: Memory algorithm.


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

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

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.