Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Garbage Collection Crash Course

Garbage Collection Crash Course

Presentation given at Rails Israel 2016.

Yorick Peterse

November 14, 2016
Tweet

More Decks by Yorick Peterse

Other Decks in Programming

Transcript

  1. Memory Management 1. Manual (C, C++, Rust, etc) 2. Reference

    Counting (Python, PHP) 3. Tracing Garbage Collection (Java, Ruby, Go, D, and many more)
  2. A tracing garbage collector determines object liveness using an object

    graph. Reachable objects are live, unreachable objects are to be reclaimed
  3. Invented in ±1959 by John McCarthy. Also known for inventing

    parenthesis Lisp, AI development, and much more.
  4. Terminology • Mutator: an application thread • Collector: a thread

    performing garbage collection • Roots: pointers directly reachable from the stack • Precise: all pointers can be identified by default, the opposite being “conservative” • Write barrier: code that runs upon writing a pointer to an object • Stop the world: an event during which all mutators are paused
  5. Garbage Collection Steps 1. Determine the roots 2. Traverse the

    roots and their child pointers 3. Record which objects are reachable 4. Reclaim memory of unreachable objects 5. Optional: compact the heap to prevent fragmentation
  6. Sweeping 1. Linearly traverse the heap 2. Check if an

    object is marked 3. Reclaim the memory if the object is not marked 4. Continue until the end of the heap
  7. Compacting To combat fragmentation objects are moved to the start

    of the heap, this is called “compacting”
  8. Heap Layout Heap divided into “blocks”, blocks are divided into

    “lines”. Multiple free aligned lines are “holes”
  9. Evacuation When a block is marked for evacuation its objects

    are moved to another block. Once a block is empty it’s returned to the global pool
  10. Young To Mature Writes Young pointers stored in mature objects

    must be tracked somehow, otherwise a young a collection would miss them
  11. What To Store? We can either store the young pointer,

    or a pointer to the mature object
  12. Storing Young Pointers Only need to store the pointer that

    was written, but we may end up storing multiple pointers written to the same mature object.
  13. Storing Mature Pointers Multiple writes store only a single mature

    pointer, but the collector may traverse unrelated mature pointers
  14. Scanning Remembered Set When collecting the young generation the collector

    also scans the remembered set for pointers, just like the roots
  15. Concurrent Writes While the collector runs the mutator may write

    new objects to objects that have already been traversed. A collector must somehow handle this
  16. Tracking Writes When writing an object a write barrier ensures

    the source/target object have the correct colour
  17. Write Barrier Techniques 6 different techniques for two types of

    write barriers. Object colors are changed or objects are scanned immediately. Too much to cover in this presentation :(
  18. CRuby • Generational (since 2.1) • Conservative (due to C

    extensions) • Uses bitmaps for marking • Stop the world • Incremental • Does not compact, instead re-uses reclaimed object slots