C # Or problems encountered in. Net's Automatic Memory Management Learning.

Source: Internet
Author: User
Tags hosting

Recently, I am reading msdn2005 and want to migrate data from VB6.0 to C #. As a beginner, I started from the basics. When I learned the "Automatic Memory Management" section of the Common Language Runtime Library, I had a few questions that I didn't understand.

Description in msdn:

Level and Performance

To optimize the performance of the garbage collector, managed heaps are divided into three generation levels: 0, 1, and 2. The runtime garbage collection algorithm is based on the following general principles. The principles of these garbage collection schemes have been proved by experiments in the computer software industry. First, it is faster to compress part of the memory of the managed heap than to compress the entire managed heap. Second, the survival time of newer objects is shorter, while that of older objects is longer. Finally, newer objects tend to be correlated and are generally accessed by applications at the same time.

The runtime Garbage Collector stores new objects in a 0th-level managed heap. Objects Created earlier in the lifetime of an application are upgraded and stored in level 1st and level 2nd managed stacks if they are not recycled. The object upgrade process is described later in this topic. Because the compression of part of the managed heap is faster than compressing the entire managed heap, this solution allows the Garbage Collector to release a specific level of memory each time a collection is executed, rather than the memory of the entire managed heap.

In fact, the garbage collector recycles when the level 1 hosting heap is full. If an application attempts to create a new object when the 0th managed heap is full, the garbage collector will find that no available address space can be allocated to the object in the 0th managed heap. The garbage collector recycles the object and tries to release the address space in the 0th-level managed heap for the object. The garbage collector starts to recycle objects from a 0th-level managed heap (instead of all objects in the managed heap. This is the most effective way because new objects tend to have a shorter lifetime and expect applications to no longer use many objects in the 0th-level managed heap when reclaim is executed. In addition, a 0th-level managed heap can be recycled separately, so that the application can continue to create new objects.

After the Garbage Collector recycles a 0th-level managed heap, It compresses the memory of accessible objects, as described in releasing memory before this topic. The garbage collector then upgrades these objects and considers this part of the 1st-level managed heap. Because unrecycled objects often have a long lifetime, it makes sense to upgrade them to a higher level. Therefore, the garbage collector does not have to re-check objects in the 0th and 1st managed heaps every time it executes the collection of 2nd managed heaps.

After you perform the first Reclaim of A 0th-level managed heap and upgrade accessible objects to a 1st-level managed heap, the garbage collector will consider the rest of the 0th-level managed heap. It will continue to allocate memory for new objects in the 0th-level managed heap until the 0th-level managed heap is full and another recycle is required. At this time, the Optimization engine of the garbage collector determines whether to check the objects at an older level. For example, if the Recycle of A 0th-level managed heap does not recycle enough memory and the application cannot successfully complete the attempt to create new objects, the garbage collector will first execute the Recycle of a 1st-level managed heap, then execute the 0th-level managed heap reclaim. If you still cannot recycle enough memory, the garbage collector will recycle 2nd, 1, and 0 managed heaps. After each collection, the garbage collector compresses accessible objects in the 0th managed heap and upgrades them to the 1st managed heap. Objects not recycled in the 1st managed heap will be upgraded to the 2nd managed heap. Because the Garbage Collector only supports three levels, objects not recycled in the 2nd-level managed heap will be retained in the 2nd-level managed heap, it is determined that they are inaccessible in future recycling.

Related information obtained from the Internet:

(3). Net Framework garbage collection mechanism

The. NET Framework contains a managed heap. All. NET languages use it when allocating reference type objects. Lightweight objects such as value types are always allocated to the stack, but all class instances and arrays are generated in a memory pool. This memory pool is a managed heap.

The garbage collector in the. NET Framework is called the generational garbage collector (generational Garbage Collector). That is to say, the allocated objects are divided into three categories, or "Generation ". 0, 1, 2, respectively. The initialization size of the hosting heap corresponding to generation 0, Generation 1, and generation 2 is 256 K, respectively, 2 m and 10 m. The garbage collector will change the size of the managed heap if it finds that changing the size can improve the performance. For example, when the application initializes many small objects and these objects are quickly recycled, the garbage collector will change the 0th-generation managed heap to 128 K and increase the recycling frequency. If the opposite is true, the garbage collector will increase the size of the managed heap when it finds that a lot of space cannot be recycled in The 0th generation managed heap. Before the application is initialized, all levels of managed stacks are empty. When an object is initialized, it is put into the 0th-generation managed heap according to the initialization sequence.

Objects with recently allocated memory space are placed in The 0th generation, because the 0th generation is very small and small enough to be placed in the second-level (L2) cache of the processor, therefore, the 0th generation can provide us with fast access to the objects in it. After a round of garbage collection, objects in the 0th generation are still moved into the 1st generation, and after a round of garbage memory collection, objects that are still in the 1st generation are moved into the 2nd generation. The 2nd generation contains objects with a long lifetime, which have been recycled for at least two rounds.

C # When the program allocates memory for an object, the managed heap can almost immediately return the memory required by the new object, the reason for the efficient Memory Allocation performance of the managed heap is that the managed heap has a simple data structure. The managed heap is similar to a simple byte array and has a pointer to the first available memory space.

When a block is requested by an object, the above pointer value is returned to the calling function, and the pointer is adjusted to the next available memory space. Allocating a hosted memory block is only a little more complex than increasing the value of a pointer. This is also one of the performance optimizations of the managed stack. In an application that does not require much garbage collection, the managed heap performs better than the traditional heap.

Due to the existence of this linear memory allocation method, objects allocated simultaneously in the C # application are usually allocated adjacent to each other on the managed heap. The arrangement is completely different from the traditional heap memory allocation. The traditional heap memory allocation is based on the memory block size. For example, the locations of the two objects allocated at the same time on the heap may be very long, reducing the cache performance. Therefore, although the memory allocation is very fast, in some important programs, the available memory in The 0th generation is likely to be completely consumed. Remember, The 0th generation is as small as the L2 buffer, and unused memory is not automatically released. If no valid memory can be allocated in generation 0th, a garbage collection trigger will be triggered in generation 0th. In this round, all objects no longer referenced will be deleted, and move the currently in use objects to the 1st generation. 0th-generation garbage collection is the most common type of garbage collection, and the speed is very fast. When the 0th-generation garbage memory recycle cannot effectively request sufficient memory, the 1st-generation garbage memory recycle will be started. The garbage memory collection of the 2nd generation is used as the final means, and only when the garbage memory collection of the 1st AND 0th generations cannot be provided with enough memory. If no memory is available after garbage collection is performed on each generation, an outofmemeryexception exception is thrown.

According to the instructions in the above two documents, I understand that after the first collection of the level 0 managed heap, all the objects that can be accessed in the Level 0 managed heap were upgraded to level 1st managed heap, in this case, the available size of the 0-level managed heap should be the original size. After Repeated collections of level-1 managed heaps are executed several times, if the level-1 managed heap is full, accessible objects in the level-0 managed heap cannot be upgraded to level-1 managed heap. In this case, check whether the available size in the 0-level managed heap meets the requirements of new objects. If yes, create new objects in the 0-level managed heap. If not, then, the level-1 managed heap is reclaimed, And the accessible objects of level-1 managed heap are upgraded to level-2 managed heap, at the same time, the accessible objects in the 0-level managed heap are upgraded to the 1-level managed heap, so that the 0-level managed heap is cleared to meet the needs of creating new objects. In this way, repeat the collection to the Level 2 hosting stack, and put down the accessible objects that must be upgraded for the Level 1 hosting heap, then the collection of level 2 hosting heap will be executed, execute Level 1 managed heap reclaim, then execute level 0 managed heap reclaim, and finally create new objects in level 0 managed heap.

Do you know this is correct ?!

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.