The DotNet Garbage Collection

來源:互聯網
上載者:User

今天開始發第一篇英文原創部落格《The DotNet Garbage Collection》

The Garbage Collection is a very important part of DotNet platform.Today we will talk about it. Think about if not GC,DotNet is also called a platform? Although both languages are compiled into MSIL in dotnet, but when the recycling operation was "fighting each other", so that the programming will not only coding difficulty, but also make extremely complicated memory management ( language processing in different small differences, will be amplified in the recovery of resources), and it is not benefit to plantform transplanting.

This article will introduce the operation mode, algorithms, and associated of DotNet garbage collection ,and will show the key method of garbage collection.

Speaking of garbage collection, very few people know, is not associated with Java garbage collection occurs, as early as in 1958, Turing Award winner John invented the Lisp language has been provided to the function of GC, which is the first time in GC, a flash of thought! Then, in 1984, Dave Ungar invented the first official language Small talk with a GC mechanism.

. Net garbage collection is a big topic, if you did not come into contact with a similar language like C + +, it is difficult to understand how GC is an important and exciting things:

1. To improve the cohesion of software systems.
2. Reduce programming complexity, so that programmers do not have to deal with memory release.
3. Without prejudice to the abstract system designers.
4. Reduce the inappropriate use of memory produced by Bug.
5. Success of the memory management managed to the runing time, changes loopholes in the management of unpredictable to can be estimated.

Body:

This chapter will introduce "GC algorithms and working methods"," GC coding method analysis ".

Section. GC algorithms and working methods

1. Algorithm

  The nature of the garbage collector is tracking all references OF the object, reorganize the objects which is no longer be used ,release current memory.

This sounds similar to something called the "reference count (Reference Counting) " algorithm, but this algorithm needs to traverse all the objects, and maintain their pointer, so some lower efficiency, and the problem of  "reference cycle" will “give you some jokes” at some time,the "reference cycle" is the reason of memory leak. So. Net used a technique called "mark and clear (Mark Sweep) " method to accomplish the above tasks.

 The algorithm of "Mark and clear ", the name suggests, has two skills:

" Mark " skills - garbage identification: the root from the application, marking the using of reference and making relationship with each other, traversal all objects which be allocated on the HEAP, the no referenced object will not be marked, its becomes garbage; survival of the object is Marked, the CLR will make (maintenance) the objects to a "root - reachable object" chart. "

In fact, CLR will maintenance those relations as a "tree", no doubt, the students understand data structures are known, structures of tree can up speed of traverse.

Checking, marking an object reference, is a very interesting thing, there are many ways to do it, but only one is optimal efficiency,. Net finish it by the stack, in the process of input and output stack.

First in the tree, select an object which is survival, all references(in running time) of the object will be push stack, ok,and then, CLR start to find another survival objec and redo above step, until the stack is empty. Stacks become empty means that CLR have been traversaled all survival object of the local root (or a tree node). Tree nodes includes local variables (local variables will be recovered very quickly, because its scope is very clear and very easy to be controlled), register, static variables, these elements have to repeat this operation. Once completed, gc will check all objects one by one and mark referred objects, then some objects become garbage because it is not be marked.

"Clear" skills - release memory: Enabling Compact algorithm, move the survival objects, change their pointers to make it contiguous in memory, so the free memory contiguous too, and it solved the problem of memory fragmentation When re-allocate memory for other object, CLR do not have to searching the free space in a debris memory, so speed of allocation will be improved.

However, large objects (large object heap) except, GC will not move a giant in-memory, because it knows that the CPU is not cheap. Normally, large objects have a long lifetime, when a large object in the. NET managed heap generation, it is assigned to a special part of the heap, because the performance improved by move large objects less than arrange the heap special.

The “Compact” algorithm can improve the speed when re-allocate memory ,if the new object in the heap allocated are compact, then the cache performance will be improved, because objects who was allocated together are often used together (the program locality principle), Therefore, a continuous space for the program is very important.

我是李鳴(Aicken) 請您繼續關注我的下一篇文章。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.