Automatic garbage collection in. net

Source: Internet
Author: User
For a long time, garbage collection has a poor reputation in the software industry. Many Program Personnel think that garbage collection is not as direct and efficient as they are. This is sometimes true. A memory reclaim method that is well designed and customized for a specific program is certainly better than providing garbage collection performance for all programs. However, the requirements for programmers are very high. the time and energy spent on the design of memory recycling for a project is considerable, and a slight carelessness will lead to catastrophic errors, skilled programmers cannot afford it, and the entire modern software industry cannot afford it. It has become the consensus of the software industry that such a common and heavy task is handed over to the system for processing, freeing programmers from focusing on the realization of transaction logic and system functions. The new. NET platform architecture launched by Microsoft introduces an automatic garbage collection mechanism. This article will detail the principle and answer how such a mechanism works for garbage collection? How to Control garbage collection? When do I need to control garbage collection? What is garbage collection that cannot be solved? And so on. NET platform.
To understand the garbage collection mechanism during. Net runtime, first we need to understand the memory allocation during. Net runtime .. Net runtime manages resources (managed resources) using heap allocation method referenced by objects. This allocation method is usually very fast. The memory of an actual system is always limited. When there are not many memory resources available for the system ,. net runtime, the following memory resources may not meet the following memory allocation requests, so it starts to recycle and release memory resources that are no longer referenced by the system .. The net Garbage Collector uses a mark and compat Algorithm . Whenever garbage collection starts ,. from the current root object (including global object, local object, static object, and CPU register object) at runtime, the net Garbage Collector starts searching for all objects referenced by the root object, these objects are the objects that are being applied when the garbage collection is run. In addition, other managed runtime objects are no longer used by the system, therefore, garbage collection can be performed. All referenced objects are copied down to the runtime managed heap and their reference pointers are modified. It should be noted that, in. when the net garbage collector moves the referenced object and changes the referenced pointer, the system cannot perform any operations on these objects. This is guaranteed by the mutex mechanism during the runtime, without the intervention of programmers.
Traversing all cited objects is often costly, and you do not have to do this. An empirical understanding is that when an object stays in the memory for a longer period of time, the more likely it will be referenced in the memory. On the contrary, the shorter an object stays in the memory, the more likely it will be collected .. Net collectors use a method called generation division to reflect this empirical memory resident theory. It divides the objects in the managed heap into three generations (generations). The first generation is the objects that have never experienced garbage collection residing in the memory. They are usually some local variables, and they have the shortest life. The second generation is the objects that have only experienced garbage collection and still reside in the memory. They are usually short-lived objects such as forms, lists, and buttons. In the third generation, objects still reside in the memory after two or more garbage collections. They are usually some application objects, which usually need to reside in the memory for a long time. When the collector starts to collect garbage, it first collects garbage for the first generation of objects, which usually releases a large memory space and often meets the following memory requests. If the collection results of this generation are not satisfactory, the second generation of reading images will be collected. If not, the third generation of garbage collection will be conducted.
The. NET garbage collection mechanism supports a concept called finalizaion. This is similar to the destructor in C ++. The termination operation clears some unmanaged resources after garbage collection is executed. It has many restrictions during. Net runtime and is often not recommended. After the programmer implements the finalizer for an object, the runtime adds the reference of this object to a linked list called the ending object reference set, as a sign for termination. At the beginning of garbage collection, if an object is no longer referenced, but it is added to the linked list of the ending object reference set, the object is marked as a terminating operation object at runtime. After the garbage collection is complete, the terminating thread will be awakened during running to perform the terminating operation. Obviously, this will be removed from the linked list of the ending object reference set. It is easy to see that terminating an operation will bring additional overhead to the system. Termination is implemented by enabling the thread mechanism, which has a thread security issue .. . Net does not guarantee the sequence of terminated execution. That is to say, if object A has a reference pointing to object B, both objects have terminated operations, however, object A does not necessarily have a valid object a reference when terminating the operation. Therefore, it is not recommended to terminate the object during. Net runtime. Only the application terminates the object when unmanaged resources, such as database connections and file opening, need to be released strictly.
Most of the time, garbage collection should be controlled by. Net runtime, but sometimes it is necessary to manually control the garbage collection operation. For example, after a large-scale object set is operated, we are sure that we will not perform any operations on these objects any more, so we can force the garbage collection to be executed immediately, by calling system. GC. the collect () method can be implemented, but frequent collection significantly reduces system performance. In another case, an object has been placed on the chain of the terminated object reference set. However, if we have already terminated the object in some places in the program, after that, you can call system. GC. supressfinalize () is used to remove object references from the end object reference set chain to ignore the termination operation. It should always be clear that the system burden of terminating operations is very heavy.
To sum up, the. NET garbage collection mechanism is responsible for recycling the managed memory resources that are no longer used by the system. It selects the collection objects and time through certain optimization algorithms. Programmers can only immediately force garbage collection when releasing a large number of managed resources. When releasing unmanaged resources, they must terminate the process. At other times, the resources can be recycled. net garbage collection.

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.