. NET garbage collection algorithm

Source: Internet
Author: User

The garbage collector detects if there are any objects in the managed heap that the application is not using, and if, after a garbage collection, the stack has no memory available, the new operator throws a OutOfMemoryException (memory overflow). Each application contains a set of roots, each of which is a storage location. which contains a pointer to the reference type object. The pointer either references an object in the managed heap, or is null. Any static field defined in a type is considered to be a root, and a value type variable must always be considered a root. The garbage collector can run along the call stack of a thread, detecting each method's internal table to determine the root of all calling methods. The first stage of the garbage collector is the so-called marking phase, in which the garbage collector is upstream along the thread stack to check all the roots. If you find a root that references an object, on the object's Synchronize Fast index field, open a----------This is how the object is labeled. The garbage collector iterates through all the available objects in a recursive manner. After you mark the object referenced by the root and its fields, the garbage collector checks the next root, continues to tag the object, and if the garbage collector tries to mark an object that was previously marked, it stops walking along that path. This behavior has 2 purposes. First, the garbage collector does not traverse a group of objects more, the index performance is significantly enhanced, and secondly, if there is a circular list of objects, you can avoid falling into an infinite loop. Once all the roots have been checked, the heap will contain a set of marked and unmarked objects. Unmarked is considered rubbish. Now the garbage collector begins the second phase, the compression phase, in which the garbage collector traverses the heap linearly to look for contiguous blocks of memory for the tagged object. If the memory blocks are found to be small, the garbage collector ignores them. If a large, usable contiguous block of memory is found, the garbage collector moves the non-garbage object here to compress the heap. After moving objects in memory, variables that contain "pointers to these data objects" and CPU registers are not valid. So the garbage collector must re-access all the root of the application and modify the new memory location that they point to the object. After the heap memory is compressed, the nextobjptr pointer to the managed pair points to the location immediately after the last non-garbage object. The garbage collector can cause significant performance damage. This is a major disadvantage of using the managed heap. But this only happens when the garbage collection is in the No. 0 generation. When did the Finalize method get called? ① No. 0 Generation full ② code shows a static method of calling System.GC Collect③windows report out of memory ④clr unload AppDomain, when the CLR considers that no root exists in the AppDomain and therefore garbage collection of all generations of objects. ⑤CLR closing a process normally terminates, the CLR shuts down, no root exists in the shutdown process, and therefore calls the Finalize method of many objects in the managed heap. Note: The CLR does not attempt to compress or free memory at this time because the entire process is terminated, and Windows is responsible for reclaiming all the memory of the process. The disadvantage of finalize: its invocation time is not guaranteed, it is not a public method, so the user of the class cannot display the call to it. 

. NET garbage collection algorithm

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.