C # garbage collection mechanism for study notes

Source: Internet
Author: User

Basic assumptions of the garbage collector:
1: The newer an object, the shorter its lifecycle. That is to say, objects that recently allocated memory space are most likely to be released.
2: The older an object, the longer its lifecycle will be. That is to say, after several rounds of garbage collection, the object still exists, and a lot of work is required to search for it, but only a small part of space can be released. So we may not have found this object, and we have obtained the required memory.
3: there is usually a reference relationship between the new object and the new object. That is to say, the objects allocated with memory are usually used at the same time, and they are connected to each other to improve cache performance and recovery efficiency.
4: Part of the compressed heap is faster than the whole heap.
The recycler in C # Is a Generational Garbage Collector (Generational Garbage Collector) that divides the allocated objects into three categories or generations. (The GC. GetGeneration method can be used to return the current generation of any object as a parameter ). Objects with recently allocated memory are placed in The 0th generation. Because the 0th generation is small enough to be placed in the second-level cache of the processor, it can provide fast access to objects. After a round of garbage collection, the objects of the 0th generation that are still retained are moved into the 1st generation. After a round of garbage collection, objects that are still in the 1st generation are moved into the 2nd generation, and objects that have a long lifetime are secured in the 2nd generation.
In C #, the median type is to allocate memory in the heap. They have their own lifecycles, so they are automatically allocated and released without being managed. The reference type is to allocate memory in the heap. Therefore, the allocation and release of the reference type need to be managed by the recycler system. C # When allocating memory to an object, the managed heap can immediately return the memory required for the new object, because the managed heap is similar to a simple byte array and has a pointer pointing to the first available memory space, the pointer moves backward like a cursor, and the segment memory is allocated to the objects of the running program. In small programs that do not require too much garbage collection, the managed heap performance is better than the traditional heap.
When there is no valid memory available for allocation in generation 0th, a round of garbage collection in generation 0th will be started. It will delete objects that are no longer referenced, and move the currently used object to the 1st generation. When the 0th-generation garbage collection still cannot request sufficient memory, the 1st-generation garbage collection will be started. If no memory is available after garbage collection is performed for each generation, an OutOfMemoryException is thrown.

 

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.