. Net CLR 4.0 improvement on the garbage collection mechanism

Source: Internet
Author: User

A survey of garbage collection and the changes CLR 4.0 brings in-series of what is new in CLR 4.0
Introduction
Garbage Collection in. net is a very important mechanism. this article will talk about the improvements CLR4.0 has made to garbage collection. to better understand these improvements, this article also introduces the history of garbage collection. in this way, we have a big impression on the whole garbage collection. this is a big impression on us. net architecture is helpful.

Garbage Collection is an important component. net. the post will talk about what has been improved in CLR 4.0. to understand it, I will take a survey of the history of garbage collection. this way we can have a big picture of garbage collection. this will help us master. net architecture in comprehensive manner.

About Garbage collection
In the C ++ era, we need to manage the applied memory and release the memory ourselves. so the new and delete keywords are available. there are some memory application and release functions (malloc/free ). the C ++ program must manage its Memory well, otherwise it will cause Memory leakage (Memory leak ). in. in the net Era, Microsoft provides a powerful mechanism for developers-garbage collection. the garbage collection mechanism is part of the CLR. We don't have to worry about when the memory will be released. We can spend more time focusing on the business logic of the application. the garbage collection mechanism in CLR uses certain algorithms to determine that some memory programs are no longer used. The memory is reclaimed and handed over to our programs for reuse.

In the times of C ++, we need to allocate and release memory by ourselves carefully, therefore there are new, delete keywords in C ++, and fuctions (malloc/free) to allocate and release memory. C ++ program has to manage its memory well, otherwise there will be memory leak. in. net, Microsoft provides a strong machanism to developers-Garbage collection. the Garbage collection is part of CLR. we do not need to worry about when to release memory. we can spend more time on buisness logic of applications. the Garbage colleciton of CLR adopts algorithms to decide which part of memory the program does not need any more, and then release these memory for further use.

Garbage collection functions The functionalities of Garbage collection
It is used to manage the memory allocation and release of hosted and unmanaged resources. In charging of the releasing and re-allocation of memory of managed and unmanaged resources.

Find the objects that are no longer in use, release the memory occupied by them, and release the memory occupied by the unmanaged resources. find the objects no longer needed, release the memory the objects occupied, and affranchise memory occupied by unmanaged resources.

After the Garbage Collector releases the memory, memory fragments appear. The garbage collector moves some objects to get the whole block of memory. At the same time, all object references will be adjusted to point to the new storage location of the object. After releasing the memory no longer needed, there is memory scrap. Garbage collector shifts objects to get consecutive memory space, and then the references of objects will be adjusted according to the shifted address of objects.

Let's take a look at how CLR manages managed resources. Let's see how CLR takes care of managed resources.

Managed heap and Managed stack:
. When running our program, net CLR opened two places in the memory for different use-managed stack and managed stack. the managed stack stores local variables and tracks program calls and responses. the managed heap is used to store reference types. the reference type is always stored in the managed heap. the value type is usually placed on the managed stack. if a value type is part of a reference type, the value type is stored in the managed heap along with the reference type. what is the value type? These types are defined under System. ValueType:

Bool byte char decimal double enum float int long sbyte short struct uint ulong ushort

When. net CLR runs our program, CLR declares two ranges of memory for different purposes. managed stack is to store local variables, and trace the call and return of routines. managed heap is to store reference types. usually value types was put on managed stack. if a value type is a part of a reference type, then the value type will be stored in managed heap along with the reference type. what are Value types? They are the types defined in System. ValueType:

Bool byte char decimal double enum float int long sbyte short struct uint ulong ushort

What is a reference type? The type declared by class, interface, delegate, object, and string is the reference type. What are reference types? The types declared with class, interface, delegate, object, stirng, are reference types.

We define a local variable of the reference type. when We assign a value to it, we declare a local variable, which is a reference type, and We assign a value to the local variable, like the following:

Private void MyMethod ()
{
MyType myType = new MyType ();
MyType. DoSomeThing ();
}
In this example, myType is a local variable. The new instantiated object is stored in the managed stack, while the myType variable is stored in the managed stack. the myType variable in the managed stack stores a reference pointing to the new instantiated object on the managed stack. when CLR runs this method, it moves the managed Stack pointer to allocate space for the local variable myType. When new is executed, CLR first checks whether the managed Stack has enough space, if it is enough, just move the pointer of the managed heap to allocate space for the MyType object. If the managed heap does not have enough space, it will cause the Garbage Collector to work. the CLR knows all types of metadata before allocating space, so it can know the size of each type, that is, the size of the occupied space.

In this sample, myType is a local variable. the object instantiated by new operation is stored in managed heap, and the myType local variable is stored in managed stack. the myType local variable on managed stack has a pointer pointing to the address of the object instantiated by new operation. when CLR executes the method, CLR moves the pointer of managed stack to allocate memory for the local variable myType. when CLR executes new operation, CLR checks first whether managed heap has enough space, if enough then do a simple action-move the pointer of managed heap to allocate space for the object of MyType. if managed heap does not have space, this triggers garbage collector to function. CLR knows all the metadata of types, and knows the size of all the types, and then knows how big space the types need.

When the CLR completes the execution of the MyMethod method, the myType local variable on the managed stack is immediately deleted, but the MyType object on the managed stack is not necessarily deleted immediately. this depends on the trigger condition of the garbage collector. this trigger condition will be introduced later. when CLR finishs execution of MyMethod method, the local variable myType on managed stack is deleted immediately, but the object of MyType on managed heap may not be deleted immediately. this depends on the trigger condition of garbage collector. I will talk about the trigger condition later.

We learned how to manage managed resources in CLR. next we will look at how the Garbage Collector looks for a hosted object that is no longer in use and releases the memory it occupies. in previous paragraphs, we learn how CLR manages managed resources. in following paragraphs, we will see how garbage collector find objects no longer needed, and release the memory.

How does the garbage collector find hosted objects that are no longer in use and release the memory it occupies? How garbage collector find objects no longer needed and release memory
We learned how to manage objects on the managed stack in CLR. the memory of the managed stack can be easily managed based on the advanced post-release principle. managed stack management is much more complicated than managed stack management. the following describes the management of managed stacks. in previous paragraphs, we learn how CLR manages the objects on managed stack. it is easy to manage managed stack as long as you utilize the rule "first in last out ". the management of managed heap is much more complicated than the management of managed stack. the following is all about the management of managed heap.

Root The root
When the Garbage Collector looks for a hosted object that is no longer in use, it determines that an object can be released if it no longer has reference to it. in some complex cases, an object points to the second object, and the second object points to the third object ,... Like a linked list, where does the spam collector start to look for hosted objects that are no longer in use? Taking the linked list as an example, it is obvious that you should start searching from the beginning of the linked list. So what are the things starting with the linked list? The criteria garbage collector uses to judge whether an object is no longer needed is that an object can be released when the object does have any reference. in some complicated cases, it happends that the first object refers to the second object, and the second object points to the third object, etc. it is looking like a chain of single linked nodes. then the question is: where does the garbage Collector begins to find objects no longer needed? For the example of the single linked node chain, we can say it is obvious garbage collector starts from the beginning of the chain. then the next question is: what are the stuff at the beginning of the chain.

Is a local variable, global variable, static variable, pointing to the CPU register of the managed heap. in CLR, they are called root. the answer is: local variables, global variables, static variables, the CPU registers pointing to managed heap. in CLR, they are called "the roots ".

What can I do next with the garbage collector? Got the roots, what will garbage collector do next?

Create a graph that describes the reference relationships between objects. Build a graph, which shows the reference relationship among objects.
The garbage collector first assumes that all objects in the managed heap are inaccessible (or are not referenced and no longer needed), and then starts from those variables on the root, for each variable on the root, find the object on the hosting stack that it references, add the object to the graph, and then search for the object to see if it references another object, if yes, add the found object to the graph. If no, it indicates that the link has been found at the end. the garbage collector starts searching for another variable on the root until all the variables on the root are found, and then the Garbage Collector stops searching. it is worth mentioning that the garbage collector has some minor optimizations during the search process. For example, because the reference relationship between objects may be complicated, it is possible to find an object, this object has already been added to the graph, so the Garbage Collector will not continue searching on this chain, but will continue searching on other chains. this improves the performance of the garbage collector.

First garbage collector supposes all the objects in managed heap are not reachable (do not have reference, or no longer needed ). then start from the variables in the roots. for each of the variable in the roots, search the object the variable refers to, and add the found object into the graph, and search again after the found object for next refered object, etc. check whether the found object has next reference. if has, continue to add the next found object into the graph. if not, it means this is the end of the chain, then stop searching on the chain, continue on next variable in the roots, keep searching on roots, until all the searching are finished. in the searching process, garbage collector has some optimization to improve the performance. like: Because the reference relationship cocould be complicated among objects, it is possible to find an object that has been added into the graph, then garbage collector stops searching on the chain, continue to search next chain. this way helps on performance of garbage collection.

After the spam collector creates this graph, the remaining objects that are not in this graph are no longer needed. the garbage collector can recycle the space they occupy. after buidling the reference graph among objects, the objects not in the graph are no longer needed objects. garbage collector cocould release the memory space occupied by the no longer needed objects.

To be continued...

References
Garbage Collection: Automatic Memory Management in the Microsoft. NET Framework By Jeffrey Richter http://msdn.microsoft.com/en-us/magazine/bb985010.aspx

Garbage Collection Part 2: Automatic Memory Management in the Microsoft. NET Framework By Jeffrey Richter http://msdn.microsoft.com/en-us/magazine/bb985011.aspx

Garbage Collector Basics and Performance Hints By Rico Mariani at Microsoft http://msdn.microsoft.com/en-us/library/ms973837.aspx

Http://drowningintechnicaldebt.com/blogs/royashbrook/archive/2007/06/22/top-20-net-garbage-collection-gc-articles.aspx

 

Dress up games dress up games makeup games beauty games costume princess games dress up fairy games dress up boyfriend games lovers appointment games wedding dress dresses games a sue games cooking games beauty restaurant games haircut games manicure games Barbie doll games big sister game Arrangement room games take care of baby games Zuma games even watching games against Touch games bubble Hall games super Mary Games gold miner games secrets room escape games magic tower games find fault games vent games double games children games Altman games sponge baby games rainbow cat blue rabbit games Doraemon games pleasant goat and gray wolf games funny games casual Games adventure games simulation management games chess and card games test games strategy games action games Sports Games agile Games shooting games puzzle games integrated games

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.