Understand the basic working principles of the garbage collection platform and the working principles of the garbage collection platform

Source: Internet
Author: User

Understand the basic working principles of the garbage collection platform and the working principles of the garbage collection platform

Understand the basic working principles of the garbage collection platform

Each program has different resources, such as files, memory buffering, screen space, network connections, and database connections. In fact, in the programming of objects on the surface, each type represents a resource available for the program,

To use these resources, you must allocate memory for these resource types. The following steps are required to access a resource:

(1) Call the newobj of the il command to allocate memory for the resource type. Use the new operator in c # To automatically generate the command.

(2) initialize the resource and set the initial status of the resource to make the resource available. The type of instance constructor is responsible for this work.

(3) Access type members to use resources.

(4) destroy the resource status for cleanup.

(5) release the memory. The garbage collector is responsible for this step.

In this simple process, how many programmers forget to release the memory they no longer need? How many programmers are trying to call the released memory?

During unmanaged programming, these two bugs of the application are much more serious than other bugs, and they are generally unable to predict their consequences. If they are other bugs, you can detect program behavior exceptions to correct them. However, these two bugs may cause resource leakage and object damage, which is unpredictable for applications.

It is usually difficult and boring to manage resources. It greatly disperses the attention of developers, making them unable to focus on the problems to be truly solved. If there is a mechanism that can simplify the work for developers, it is a good thing. Fortunately, there is a mechanism in it, which is garbage collection (GC-garbage collection for short ).

With GC, developers do not have to track memory usage or know when to release memory. However, garbage collection knows nothing about the type resources in the memory. This means that GC does not know how to perform Step 4: destroy the resource status for cleanup. To clear resources correctly, developers must write code that knows how to clear resources correctly. These codes are usually put in the Finalize, Dispose, and close methods, but GC provides some help so that developers can skip step 4 most of the time.

 

In addition, the resources represented by value type, set type, String, Attribute, Delegate, and Exception do not require special cleaning.

If a type represents unmanaged and local resources (such as files, database connections, sockets, mutex, bitmaps, and icons), you must clear some code when preparing to recycle objects.

2. Allocate resources from managed stacks

CLR requires that all resources hosting code be allocated in the managed heap. This heap is very similar to the heap in C runtime, but you should never delete objects from the managed heap (objects not required by the application will be automatically deleted), which leads to a problem: how does managed heap know that an object is not required by an application?

At present, there are many Garbage Collector algorithms. Different algorithms are optimized for different environments and have provided the best performance in such environments. This series will focus on the garbage collection algorithm adopted by Microsoft. NET Framework CLR.

As mentioned above, new is responsible for creating an object, which causes the compiler to generate a newobj command. This command causes CLR to execute the following steps.

(1) number of bytes required for computing (and all base types) resources.

(2) plus the number of bytes required for the object overhead. Each object has two bytes of Overhead: A type object pointer and a synchronized block index (The Name Translation on different documents may be different, in the subsequent sections, I will explain these two different expenses. You can also go to the MSDN website to find some information ), the Bytes allocated to 32-bit applications and 64-bit applications are different.

(3) CLR checks whether the reserved area can provide the number of bytes required for object allocation. If the managed heap has enough space, the object will be put in. Here there is a NextObjPtr pointer, this object is put at the address pointed to by this pointer. In addition, the number of bytes allocated to the object will be cleared, and the type instance constructor will be called. The IL command newobj will return the object address. Before returning the address, the value of the NextObjPtr pointer will be added with the number of bytes occupied by the object. In this way, a new value will be obtained, pointing to the next object and putting it into the managed heap address.

For comparison, let's take a look at how the heap is allocated during C runtime. In the C Runtime heap, allocate memory for objects to traverse a linked list composed of data structures. Once a large block is found, the block is split and the pointer of the linked list node is modified to ensure the integrity of the linked list. For managed heaps, assigning objects only requires adding a value to the pointer, which is obviously much faster. In fact, the managed heap allocates objects at almost the same speed as allocating memory from the thread stack. In addition, most heap (including the C Runtime heap) are allocated where they find the available space. Therefore, several objects can be created consecutively, which may be dispersed. The address space separated by several MB in the middle. However, the continuous allocation of objects in the managed heap ensures that their object addresses are continuous.

Combined appeal, it seems that the managed heap achieves much better simple rows and speeds than the C Runtime heap. However, you have a special note on the details. The reason why the managed Stack has so many advantages is that it makes a bold assumption: "address space and storage are unlimited ". This assumption is ridiculous. Therefore, the managed heap must use a mechanism to allow such assumptions. This mechanism is garbage collection.

When an application calls new to create an object, it may not have enough address space to allocate the object. The managed heap adds the required bytes to the address in the NextObjPtr pointer to check this situation. If the result value exceeds the end of the address, the managed heap is full and garbage collection is required.

Note: this series of articles is from <CLR via C #> Third Edition

 

Related Article

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.