.NET Garbage Collector Basics

This question already has an answer here:

  • Understanding garbage collection in .NET 3 answers

  • Here's a couple of useful articles:

  • Garbage Collection: Automatic Memory Management in the Microsoft .NET Framework
    (by Jeffrey Richter)
  • Garbage Collector Basics and Performance Hints
    (by Rico Mariani)

  • You are right in some cases. The GC looks through the heap pessimistically - ie it sets off assuming everything (in Generation 0) will be GCed.

    It literally goes through everything on the heap through a first sweep called "marking", in which is checks if anything is referencing it. Since they are all reference types and some reference others, it will recursively navigate the references. Don't worry - there is logic to not get into an infinite loop!

    If it finds an object is not referenced, it will firstly mark it, by setting a flag within the object called the sync block index.

    After going through every object on the heap, it will then begin a process called "compacting" which is when it shifts all of the remaining objects into the same area of memory, leaving the memory above clear. It will keep the objects of the same generation together as they are statistically more likely to be de-referenced at the same time.

    This therefore will reduce the memory needed.

    Garbage Collection doesn't necessarily speed up your program, but does allow it to re-use the space occupied by unused objects.

    There are many many articles on the subject. I personally like "CLR via C#" by Jeffrey Richter who gives an excellent chapter on how it works.


    I'm currently reading this book to help with an independent study project in Garbage Collection at my university. If you really want to understand the ins-and-outs of Garbage Collection, I suggest reading this book because it seems to be the best one around. This most likely contains more information than what you're looking for, but it may be helpful if you want to write a Garbage Collector in the future.

    链接地址: http://www.djcxy.com/p/79092.html

    上一篇: GC移动对象时Object.GetHashCode如何工作?

    下一篇: .NET垃圾收集器基础