table of Contents
l
l about garbage collection
l garbage collection algorithm
M application roots (Application Roots)
l implementation
M stage i: tag (Mark)
M Phase II: Correction (Compact)
l Termination
l Optimization of garbage recovery performance
M weak reference (Weakreference)
M generation (Generations)
l Garbage recycling related myth
preface
Microsoft claims that .NET is a revolutionary programming technology. Many elements make it the first choice for most developers. This article we will discuss a very important advantage in the .NET Framework - the convenience of memory and resource management.
About garbage collector
Each program uses a certain order resource, or a memory buffer, or network connection, or database resources, and more. In fact, in an object-oriented environment, each type is considered to be some effective resources for the program. In order to use these resources, some memory must be assigned to describe this type.
Resource Access Press the steps:
1. For the type allocation memory to describe resources.
2. Initialize resources, set resources into initialization status to make resources available.
3. Use resources by accessing the type of instance (repeated as needed).
4. Destroy resource status to clear resources.
5. Release memory.
The garbage collector (GC) in .NET completely helides developers from tracking memory usage and determination.
Microsoft® .NET CLR (Public Language Runtuance) requires all resources to allocate from managed stacks. You don't have to release objects in the hosted stack - the object will be automatically released when the application no longer needs these objects.
Memory is not unlimited. Garbage collectors need to perform recycling to release memory. The optimization engine of the garbage collector will choose the best recovery time for the allocated distribution (accurate standards are provided by Microsoft). When the garbage collector performs recycling, it first identifies the object that is no longer used by the application in the managed heap and then performs the memory space of these objects.
However, in order to perform automatic memory management, the GC must know the seat of the root. That is, it should know when an object is no longer used by the app. In .NET, GC is learned through a thing called metadata. Each data type used in .NET describes it through metadata. With the help of metadata, the CLR knows the layout of each object in memory, and helps GC in the stages of garbage collection. Without this information, GC will not know where an object ends and the next start.
Garbage collection algorithm
Application Roots
Each application has a set of roots (ROOTS). Root identifies the storage location, this location or pointing to a hosted heap, or pointing to an empty object (NULL).
such as:
l All global and static object pointers in an application.
l All local variable / parameter object pointers in a thread stack.
l The object pointer registered in the hosted heap.
l The object pointer in the FREACHABLE queue.
The table of the active root is maintained by the JIT compiler and CLR and is accessible to the aggregation of the garbage collector.
achieve
The garbage recovery in .NET is implemented with tracking recovery, the exact CLR implements the Mark / Cash (COPACT) recovery.
This method has the following two phases:
Phase I: Mark (Mark) Find memory that can be recovered.
When the GC starts running, it assumes that all objects in the stack are garbage. In other words, it assumes that the root of the application does not point to any object in the stack.
Phase I includes the following steps:
1. GC recognizes the reference or application root of the survival object.
2. Start traversing from the root, build a picture of all objects that can be traversed from the root.
3. If GC is ready to try to add an object already in the figure, it stops traversal of this path. There are two purposes in this way, the first is greatly optimized because it does not traverse a set of objects. The second is to prevent a dead cycle when there is an object's loop connection list, so the loop is effectively controlled.
Once all roots are checked, all the trash recovers contains all objects that can be traversed from the application root. Any objects that are no longer can't be accessed by the application, that is, the so-called garbage.
Phase II: Correction (Compact)
Move all survived objects to the end of the stack, empty the space at the top of the heap.
Phase II contains the following steps:
1. Now GC linearly traverses the pile, looking for neighboring garbage pairs (now being considered free space).
2. The GC then moves the non-garbage object in the memory and removes all the gaps in the heap.
3. Objects in mobile memory cause the object pointer to fail. Therefore, GC needs to modify the root of the application to point to the new location.
4. In addition, if the object contains a pointer to other objects, the GC is also responsible for correcting these pointers.
After all garbage is identified, all non-spam objects are also organized, and all non-garbage objects are also modified, and the last non-garbage object will refer to the position of the next object.
FINALIZATION
The .NET Framework's garbage collector can track the life cycle of the object created by the application, but it is powerless when it encounters an object packaged with unmanaged resources (such as files, windows, network connections, etc.).
Once the application is no longer using those non-hosting resources, they need to release them. .NET Framework provides an endize method: When the spam is recovered, the method of the object must be performed to clear its unmanaged resources. Due to the default Finalize method, it is not possible, if you need to display the clear resource, you must overwrite this method.
If a Finalize method is used as a function of only C , it is not surprising. Although they are all given the task of the resource occupied by the object, they still have a very different semantics. In C , when the object is introduced, the pattern function is called immediately, and the Finalize method is called when the garbage is started.
In .NET, since the presence of the finalizer makes the work of garbage recovery more complex because it adds many additional operations before release the object.
Whenever, when allocating a new object with a Finalize method, there will be a pointer to this object being added to a internal data structure called the Finalization queue. When the object cannot be traversed again, the GC thinks that this object is garbage. GC first scan the Finalization queue to find the pointer to these objects, when the pointer is found, remove it from the Finalization queue and add it to another internal data structure called the Freachable queue, so that this object is no longer part of the garbage. At this time, GC completed determining garbage. Then sorted (Compact) recoverable memory, which is responsible for emptying the FINALIZE method for the FREACHABLE queue and executes the object.
When the second garbage collector is triggered, it treats the Finalize object as a real garbage, then simply releases their memory. It can be understood that when an object needs to end, it will die first, then survive (resurrection), and then die again. It is recommended to avoid using the Finalize method unless there is a need. The Finalize method will increase the memory pressure because the memory and resources occupied by the object will be released until the two garbage recycles are started. Therefore, you can't control the order of the Finalize method execution, which may result in an unpredictable consequence.