[转] Talk about the automatic garbage collection mechanism in .NET

xiaoxiao2021-03-06  50

Talking about the mechanism of .Net automatic garbage collection (Garbage Collection): cornfield

It has always been, and the reputation collection in the software industry is not good. Many programmers think that garbage collection is not as direct, efficient. This saying is some time, one carefully designed a customized memory recovery method for their own specific procedures, and is definitely higher than those who provide garbage collection performance. However, the time and energy on the programmer are very high, and the time and energy flowers in memory recycling are very considerable, and a little inadvertent will cause a catastrophic mistake, and the technician with high technology is not Starting, the entire modern software industry is also can't afford it. Turn thus universal and heavy tasks to system processing, which will be discharged from the programmer to the implementation of transaction logic and system functions, has become a consensus. Microsoft's new .NET platform architecture introduced automatic garbage collection mechanism, this article will anatomize the principle, answer how garbage recovery work? How to control garbage collection? When do you need to control garbage collection? What is garbage recovery? Waiting for important issues, providing design-based references for system developers under .NET platform. To figure out the garbage collection mechanism of .NET runtime, you must first need to figure out the memory allocation of the .NET runtime. The .NET runtime uses the HEAP allocation method using the managed resource. This distribution method is mostly very fast. A actual system is always limited. When the residual memory resources of the system are not available, the .NET runtime will "foresee" to the following memory resources will not meet the following memory allocation requests, so it The garbage collection releases the memory resources that are no longer referenced are not recited. The .NET garbage collector uses an algorithm called "marker tightening". Whenever garbage collection starts, .NET garbage collector from runtime current root objects (including global objects, local objects, static objects, CPU register objects), start looking for all objects referenced by root objects, these objects are Objects are being applied during garbage collection, remove these, other managed runtime objects, the system is no longer used, so we can collect garbage collection. All reference objects are copied down to Runtime Management Heap while modifying their reference pointers. It should be noted that when the .NET garbage collector moves reference objects and changing the reference pointer, the system cannot have any operation on these objects, which is guaranteed by the mutex mechanism at runtime, no programmer interference. Traversing all the costs of all subjects are often huge, but do not do this. A understanding of an experience is that when an object resides in memory, then it is possible to continue to be referenced in memory. Instead, an object resides in memory, the more likely it is possible to collect. The .NET collector uses a method called "generation Division) to reflect the memory resident theory of this experience. It divides the objects in the pile into three generations (Generations). The first generation has not experienced garbage collection resides in memory. They are usually some local variables, their lives are shortest. The second generation is that only a garbage collection still resides in memory, which is usually some objects such as forms, lists, buttons. The third generation is an object that is still residing in memory after two or more garbage collection, which is usually some application objects, and they tend to stay in memory for a long time.

转载请注明原文地址:https://www.9cbs.com/read-81903.html

New Post(0)