Garbage collection in. net
By Amit Kukreja
Http://www.codeproject.com/dotnet/garbagecollection.asp#xx821174xx
Garbage collection in. net
This article describes how garbage collection is executed in. net!
This is critical, although many people think it is too theoretical, just like the textbook on compilation principles in Universities (koffer note)
Content directory:
Introduction
About garbage collection
Garbage collection Algorithm
Application Root
Execution, Application
Phase 1: Mark
Phase 2: Collect and lock the memory
Release resources
Garbage collection Optimization
Weak reference
Generation
Views on garbage collection
Introduction
. Net is a revolutionary technology that Microsoft brings to the program community. Many factors have prompted most developers to use. net. In this article, we will discuss a basic advantage of. NET Framework-the flexibility of memory and resource management.
About garbage collection
Every program uses such resources as memory space, network connection, and database resources. In fact, in an object-oriented environment, each type represents a program's available resources. To use these resources, you must allocate a certain amount of memory to point to it.
To access a resource, perform the following steps:
1. allocate memory for a type of resource, which indicates this resource.
2. initialize the memory and assign the initial value to the resource to make it an available resource.
3. Use instance members of this type to access this resource.
4. Clear the resource status and Release System Resources
5. release memory space
(Every time you encounter this kind of translation, you will think of the professionalism of foreign programmers. I. e. What will happen if I want you to talk about this process? Try it! -- Koffer note)
The. NET Garbage Collector eliminates the need for program developers to track memory usage and to know when to release the memory.
Microsoft's. Net CLR requires that all resources be allocated to the managed heap. You don't need to release the objects hosting the heap at all, because they will be automatically released when the application does not need it!
The memory is not infinite. To release a certain amount of memory, the garbage collector must perform collection operations. The garbage collector is constantly optimizing the engine to determine the best execution point of the garbage collection (the exact critical value is closed by Microsoft), which is based on the allocated memory. When the Garbage Collector collects data, it checks the objects not used by the application in the managed heap, and then performs necessary operations to reclaim the memory.
However, for automatic memory management, the garbage collector must know the root location, that is, it should know when an object will not be used in the program. In. in. net, GC knows this because of metadata (metadata ). in. every data type in the. NET software contains metadata to describe it. With metadata, the CLR knows the exact location of each object in the memory, this helps the Garbage Collector to be busy in the compression and collection memory phase. If the garbage collector does not understand this, it is impossible to know the end of an object instance and the start of another object.
Garbage collection Algorithm
Application Root
Each application has a group of root nodes. The root determines the storage location, which points to the objects in the managed heap, or if it is set to null, it points to an empty node.
For example:
All global and static object pointers in the application.
Any local variable or parameter pointing to the thread stack.
Registers containing pointer objects in any managed heap.
Pointers to the objects from freachable queue
The activity root list is maintained by the JIT compiler and Clr and can be accessed by the garbage collector algorithm.
Run
The garbage collection Algorithm in. NET is implemented by tracking and executing the CLR tag/sorting algorithm.
This method contains the following two phases:
Phase 1: Mark
Find memory that can be released.
When the Garbage Collector starts running, it is assumed that all objects are junk. That is to say, assume that in this heap, no application is directed to any other object at the root.
The following steps are included in the first phase:
1. GC confirms the reference and application root of the activity object.
2. traverse all the roots to construct a graph that can reach other objects from the root.
3. If GC tries to add an object that already exists in the graph, it will stop traversing from this path. This has two purposes: on the one hand, it significantly improves the efficiency because it does not traverse a group of objects multiple times. In addition, to avoid infinite loops, you will surely see a ring join object table. Then, we can solve the ring issue well.
Once all the root nodes have been checked, the spam collector graph contains all the objects that can be accessed from the application root node in some way; in that case, any object not in the graph is an object node that is not accessed in the application, that is, they are considered as spam.
Stage 2: Sorting
Move all active objects to the bottom of the heap and leave idle objects in the top of the heap.
The second stage includes the following steps:
1. Now the Garbage Collector linear traversal heap, looking for continuous junk object blocks (this is the idle area)
2. the garbage collector moves non-junk objects downward in the memory to remove all gaps in the heap.
3. Moving an object in the memory will invalidate the pointer to the object. Therefore, the garbage collector modifies the application root so that the pointer points to a new location.
4. In addition, if any object contains a pointer to another object, the garbage collector will correct the pointer.
After all the spam are confirmed, the non-spam is tightened together, and the non-spam pointer is modified. Only after a non-spam object confirms its position, the following object can be added up.
Release resources
. The garbage collector of the Net Framework secretly tracks the lifecycle of objects created by the application. However, if an unmanaged Resource (such as a file, window, or network connection) is encountered, it will fail.
These unmanaged resources must be explicitly released after the program is executed ,. net Framework provides object. finalize method: the spam collector must be executed before the object is reclaimed. The object is used to clear its own unmanaged resources. Because the Finalize method does not do anything by default, if you need to clear it, you must override this method.
If you think that finalize is just another name of Destructors in C ++, it is not very strange. Although they are all granted the responsibility to release object resources, they have a very large semantic meaning. In C ++, once the object exceeds a certain local area, desturctor runs immediately. However, the Finalize method is executed only when the Garbage Collector method begins to clear an object.
In. net, the potential existence of finalizer adds additional steps before releasing an object to complicate the garbage processing.
No matter when a new object with the Finalize method is assigned to the stack, a pointer to the object is put into an internal "Terminate queue). When an object cannot be accessed, the garbage collector considers the object as garbage. The garbage collector scans the release queue to find pointers to these objects. Each time a pointer is found, the pointer is deleted from the release column, transfer it to another internal data structure called "freachable queue", so that this object is no longer garbage. Here, the spam collector has completed the spam validation, and then executes the Finalize method of each object to sort out the memory that can be recycled, it starts a special real-time thread to clear "freachable queue ".
The next time the garbage collector is called, it will find that these objects are real garbage and the memory they occupy will be simply released.
That is to say, when an IE Object needs to be released, it will first die, then live (Revival), and finally die. We recommend that you avoid using the Finalize method unless necessary. The Finalize method increases the memory pressure because it does not allow a released object to release memory and resources until two garbage collections are executed. Because you cannot control the execution sequence of the Finalize method, it will lead to unexpected results.
Garbage collection behavior Optimization
Weak reference
Generation
The following content is not translated, because a similar article has been found on csdn, so you can refer to it.
Http://www.csdn.net/develop/read_article.asp? Id = 19987
Http://www.csdn.net/develop/read_article.asp? Id = 19679
Http://www.csdn.net/develop/read_article.asp? Id = 19842
Thanks to the author and friends above!