Garbage collection algorithms

8,803
-1

Published on

Presentation about various algorithms used for automatic garbage collection.

Published in: Technology
0 Comments
10 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
8,803
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
313
Comments
0
Likes
10
Embeds 0
No embeds

No notes for slide

Garbage collection algorithms

  1. 1. Garbage Collection Algorithms<br />By Achinth Anand Gurkhi<br />
  2. 2. What is Garbage Collection?<br />Garbage Collection is the process by which unused objects are deleted to reclaim memory space<br />Invented by John McCarthy around 1959 to solve problems in Lisp<br />It is used in Lisp, Smalltalk, Eiffel, Haskell, ML, Schema, Modula-3, Java and .NET<br />
  3. 3. GC Algorithms<br />Reference Counting<br />Mark-Sweep Collector<br />Copying Collector<br />Mark-Compact Collector<br />Generational Collector<br />
  4. 4. Reference Counting<br />Needs help from compiler and the program to maintain a reference count<br />Compiler adds code to increment/decrement reference count when the object is referenced/dereferenced<br />If reference count is zero it is garbage collected and reference count of all objects it references is decremented by one<br />Used by ANSI C++ library classes like string<br />
  5. 5. Reference Counting<br />Advantages: <br />Garbage collection can be immediate<br />Disadvantages:<br />Cannot handle cyclic references <br />Need extra memory for the reference counter<br />Incrementing and decrementing reference counts every time a reference is created or destroyed can significantly impede performance. Example: array processing<br />
  6. 6. Mark-Sweep Collector<br />All application threads are stopped<br />Mark: from the roots (objects referenced directly) every referenced object is visited and marked<br />Sweep: entire heap is scanned and all unmarked objects are collected. Next all marked objects are reset<br />
  7. 7. Mark-Sweep Collector<br />Advantages:<br />Can handle cyclic references<br />No burden on the compiler or the application<br />Disadvantages<br />As entire heap is scanned, pauses would be longer<br />If heap is paged can have performance issues<br />Causes heap fragmentation which could lead of out of memory errors<br />
  8. 8. Copying Collector<br />The heap is divided into two equal spaces<br />One contains active data and the other is inactive<br />When the active half gets full, the world is stopped, live objects are moved to the inactive half and then the active half is cleared<br />For the next cycle, roles are reversed, the inactive half becomes the active half<br />
  9. 9. Copying Collector<br />Advantages:<br />Only live objects are visited, garbage objects are not visited<br />Data compaction is achieved which reduces cost of object allocation and out of memory errors<br />Disadvantages:<br />Requires twice the heap size than other collectors<br />Overhead of copying objects from one space to another<br />Overhead of adjusting all references to the new copy<br />Long lived objects are copied back and forth on every collection<br />
  10. 10. Mark-Compact Collector<br />All application threads are stopped<br />Mark: from the roots (objects referenced directly) every referenced object is visited and marked<br />Compact: entire heap is scanned and all unmarked objects are collected. Next all marked objects compacted at the bottom of the heap and then the flags are reset<br />
  11. 11. Mark-Compact Collector<br />Advantages:<br />Compaction is achieved <br />Without the hassle of long lived objects being copied back and forth<br />Without the need for double the heap size<br /> Disadvantages<br />Overhead of copying objects (for compaction)<br />Overhead of adjusting all references to the new copy<br />
  12. 12. Generational Collector<br />98% of the objects die young <br />Copying Collectors perform well with short-lived objects<br />Mark-Compact Collectors perform well with long-lived objects<br />Can we use different GC algorithms based on object’s age?<br />
  13. 13. Generational Collector<br />The heap is divided into generations (usually 2 or 3)<br />Objects are created in the young generation (gen 0)<br />When memory is needed, a GC of gen 0 is performed & live objects are moved to the next generation (gen 1) & dead objects are collected (Copying Collector). If enough memory is now available GC is stopped<br />Else the next older generation (gen 1) is collected. This goes on till enough memory is released or till the last (oldest) generation is reached. <br />The last generation (old objects) uses Mark-Compact Collector algorithm<br />
  14. 14. Generational Collector<br />Advantages:<br />GC cycles are small as all objects are not collected<br />Only old objects are copied from one generation to another<br />Entire heap is not scanned<br />Disadvantages<br />Overhead of copying objects (for compaction)<br />Overhead of adjusting all references to the new copy<br />

×