Ramble. Automatic garbage collection (garbage Collection) mechanism in net (turn)

Source: Internet
Author: User
Tags execution garbage collection reference resource thread
Author: Cornfield
Ramble. Automatic garbage collection (garbage Collection) mechanism in net
Garbage collection (garbage Collection) has long been a bad name in the software world. Many programmers think that garbage collection is not as straightforward and efficient as it is for them. This is sometimes true, and a memory-recovery method tailored to your specific programming is certainly more expensive than providing garbage collection for all programs. But that's very demanding for programmers, and the time and effort of a project to spend on the design of memory recycling is considerable, and a bit of carelessness can lead to catastrophic errors that cannot be affordable by highly skilled programmers and cannot be affordable by the entire modern software industry. It has become the consensus of the software industry to put such a common and onerous task to the system and to free the programmer from the realization of business logic and system function. Microsoft's new launch. NET platform architecture introduced the automatic garbage collection mechanism, this article will dissect the principle in detail, answer such as garbage collection how to work? How to control garbage collection? When do I need to control garbage collection? What is garbage collection can't solve? and other important issues, provide design-time reference for system developers under the. NET Platform.
To get a clear idea of the. Net Runtime's garbage collection mechanism, it needs to be understood first. NET runtime memory allocation. The. NET runtime uses an object-referenced heap allocation (Heap allocation) method for a managed resource (Managed Resource). Most of the time this allocation method is very fast. The memory of a real system is always limited, when the system's remaining allocated memory resources are not long, the. Net runtime "foresees" that the following memory resources will probably not meet the following memory allocation requests, so it begins to perform garbage collection to release memory resources that are no longer referenced by the system. NET garbage collector uses an algorithm called "Flag tightening" (Mark and Compat). Whenever garbage collection starts,. NET garbage collector from the runtime's current root object, including global objects, local objects, static objects, CPU register objects, and start looking for all objects referenced by the root object, which are objects that are being applied when the garbage collection is run, except for the other managed runtime objects (Managed Runtime object, which is no longer used by the system, can be garbage collected. All referenced objects are copied down to the runtime by Tang (Runtime Managed Heap), while their reference pointers are modified. It is important to note that when the. NET garbage collector moves the referencing objects and changes the reference pointers, the system does not have any action on these objects, guaranteed by the runtime's mutex, without programmer intervention.
The cost of traversing all of the currently cited objects is often enormous, and it is not actually necessary to do so. An empirical understanding is that the longer an object resides in memory, the more likely it is to remain in memory as a reference. Conversely, the shorter an object resides in memory, the more likely it is to be collected. NET Collector uses a method called "Generation division" to embody the memory-resident theory of this experience. It divides the objects in the managed heap into three generations (generations). The first generation is the object that has not experienced garbage collection residing in memory, they are usually some local variables, their life is shortest. The second generation is an object that has only experienced a garbage collection that still resides in memory, and is usually a shorter life object, such as a form, list, button, and so on. The third generation is the object that is still residing in memory after two or more times of garbage collection, usually the application objects, which often reside in memory for a long time. When the runtime collector begins to perform garbage collection, it first collects garbage from the first generation of objects, which typically frees up large memory space and often satisfies the following memory requests. If the results of this generation are not satisfactory, then the second generation of reading will be collected and, if not ideal, a third generation of garbage collection.
. NET garbage collection mechanism supports a concept called finalization (finalizaion). This is kind of like a destructor in C + +. The termination of the operation of garbage collection after the execution of some unmanaged resources cleanup work, it in the. NET runtime has a lot of restrictions, often not recommended to implement. When a programmer implements a Terminator (Finalizer) on an object, the runtime adds a reference to the object to a linked list called the terminating object reference set, as a flag to request finalization. When garbage collection starts, if an object is no longer referenced but it is joined to a linked list that terminates the object reference set, the runtime flags this object as a requirement to terminate the Action object. When garbage collection is complete, the terminating thread is awakened by the runtime to perform the abort operation. Obviously this is going to be deleted from the list of terminated object reference sets. It is easy to see that the termination operation will incur additional overhead for the system. Finalization is done by enabling threading, which has a thread-safe problem. NET runtime does not guarantee the order in which execution is terminated, that is, if object A has a reference to object B, two objects have a terminating operation, but object A does not necessarily have a valid object a reference when terminating the operation. So. NET runtime does not recommend the termination of the operation of the object, but only in the unmanaged resources such as the database connection, file opening, etc. need to be strictly released when the application of the termination operation.
Most of the time, garbage collection should be handed over. NET runtime to control, but sometimes, you may need to artificially control the garbage collection operation. For example, after manipulating a large set of objects, we are convinced that no more operations are done on those objects, so we can force the garbage collection to execute immediately, which can be achieved by invoking the System.GC.Collect () method, but frequent collection can significantly degrade the performance of the system. There is also a situation where an object has been placed on the chain of the terminating object reference set, but if we have done something to terminate in some part of the program, then we can call System.GC.SupressFinalize () after that. To remove a reference from the object reference set chain from the terminating object to ignore the abort operation. It should always be clear that the system burden of terminating operations is heavy.
Sum up. The net garbage collection mechanism is responsible for reclaiming the managed memory resources that the system no longer uses, and it chooses the collected objects and time by a certain optimization algorithm. Programmers can make immediate mandatory garbage collection when releasing a large number of managed resources, and terminate operations when releasing unmanaged resources, and other times to recycle resources. NET garbage collection up to do.
Hope that the vast number of netizens to discuss the thread!


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.