gc algorithm inside .net luo bingqiao 5/22/2009
DESCRIPTION
GC Algorithm inside .NET Luo Bingqiao 5/22/2009. Agenda. 经典基本垃圾回收算法 CLR 中垃圾回收算法介绍 SSCLI 中 Garbage Collection 源码分析. 经典基本垃圾回收算法. Reference Counting 算法 Mark-Sweep 与 Mark-Sweep-Compact 算法 Copying 算法. Reference Counting 算法. Storing the Number of reference, Pointers, and resource such - PowerPoint PPT PresentationTRANSCRIPT
![Page 3: GC Algorithm inside .NET Luo Bingqiao 5/22/2009](https://reader036.vdocuments.net/reader036/viewer/2022062408/56813fd0550346895daaafdb/html5/thumbnails/3.jpg)
经典基本垃圾回收算法
1. Reference Counting算法2. Mark-Sweep与Mark-Sweep-Compact算法3. Copying 算法
![Page 4: GC Algorithm inside .NET Luo Bingqiao 5/22/2009](https://reader036.vdocuments.net/reader036/viewer/2022062408/56813fd0550346895daaafdb/html5/thumbnails/4.jpg)
Reference Counting算法Storing the Number of reference, Pointers, and resource such
as an Object or Memory block.
• Simple reference counting
• Deferred reference counting
• One-bit reference counting
• Weighted reference counting
![Page 5: GC Algorithm inside .NET Luo Bingqiao 5/22/2009](https://reader036.vdocuments.net/reader036/viewer/2022062408/56813fd0550346895daaafdb/html5/thumbnails/5.jpg)
Reference Counting算法
Advantages and Disadvantages
• Reclaim objects promptly
• Difficult to resolve circular references
RC=1
RC=1
RC=1
RC=1
RC=1
RC=1Examples of Use:
• COM, Cocoa, Delphi, PHP, Python
![Page 6: GC Algorithm inside .NET Luo Bingqiao 5/22/2009](https://reader036.vdocuments.net/reader036/viewer/2022062408/56813fd0550346895daaafdb/html5/thumbnails/6.jpg)
Mark-Sweep
• Initially, allocate objects on the heap sequentially
• At some stage, mark the objects that are dead and can be removed
• Free the dead object slots at some stage
![Page 7: GC Algorithm inside .NET Luo Bingqiao 5/22/2009](https://reader036.vdocuments.net/reader036/viewer/2022062408/56813fd0550346895daaafdb/html5/thumbnails/7.jpg)
Mark-Sweep
Advantages and Disadvantages
• Minimal house-keeping overhead (just one free list)
• Every allocation request requires a walk thru the free list, makes allocations slow
• Heap fragmentation
Examples of Use:
• C Runtime Heap, .NET Micro Framework
![Page 8: GC Algorithm inside .NET Luo Bingqiao 5/22/2009](https://reader036.vdocuments.net/reader036/viewer/2022062408/56813fd0550346895daaafdb/html5/thumbnails/8.jpg)
Copy and Collect
• Keep two heaps
• Allocate only from one heap
• When collection is triggered on the heap, copy all alive objects to the second heap
• Switch the roles of heaps
![Page 9: GC Algorithm inside .NET Luo Bingqiao 5/22/2009](https://reader036.vdocuments.net/reader036/viewer/2022062408/56813fd0550346895daaafdb/html5/thumbnails/9.jpg)
Copy and Collect
Advantages and Disadvantages
• Conceptual Simplicity
• Copy operation needs to be done for all objects
• Blocks a lot of memory unnecessarily
![Page 11: GC Algorithm inside .NET Luo Bingqiao 5/22/2009](https://reader036.vdocuments.net/reader036/viewer/2022062408/56813fd0550346895daaafdb/html5/thumbnails/11.jpg)
GC Algorithms in advanced OO language VMS
• Mark Sweep Compact / Train algorithm
• Generational incremental Collector
• Large Object Heap
• Segments
• Finalization in CLR
• Weak References
• Pinning
• Object Layout
![Page 13: GC Algorithm inside .NET Luo Bingqiao 5/22/2009](https://reader036.vdocuments.net/reader036/viewer/2022062408/56813fd0550346895daaafdb/html5/thumbnails/13.jpg)
Overall of GC Algorithm
• Mark bit set, and if required Pin bit also set
Mark
• Grow or shrink the heap as needed
Plan• Objects are
moved to the new addresses
Compact
![Page 15: GC Algorithm inside .NET Luo Bingqiao 5/22/2009](https://reader036.vdocuments.net/reader036/viewer/2022062408/56813fd0550346895daaafdb/html5/thumbnails/15.jpg)
Mark Phase
Separate the live objects from dead objects for the
generation being collected.
• All small dead objects have their Mark bit set, and if required Pin bit also set
• Finalizable objects are put on the FReachable queue
• Weak pointers to dead objects are nulled
• All large dead objects are put on the FreeList
![Page 19: GC Algorithm inside .NET Luo Bingqiao 5/22/2009](https://reader036.vdocuments.net/reader036/viewer/2022062408/56813fd0550346895daaafdb/html5/thumbnails/19.jpg)
More Information
• ExternalISMM forum<<Garbage Collection>>, Algorithms for automatic
Dynamic Memory managements• Email