Junior Track Roadmap

Ruby: garbage collection

This course covers tools for watching and steering Ruby's garbage collector on your own objects. ObjectSpace.each_object counts the live instances of one class, and ObjectSpace.count_objects breaks the heap down by internal type. GC::Profiler records each GC pause with its duration in milliseconds. Two RUBY_GC_* environment variables are tested on Ruby 4.0.1: one has a measurable effect, and the other is deprecated. The course also covers finalizers, including the trap of referencing the object being finalized, and WeakRef, a reference the GC does not count.

What you'll learn

  • ✓Counting live objects of a class with ObjectSpace.each_object
  • ✓Comparing heap snapshots by type with ObjectSpace.count_objects
  • ✓Measuring GC pauses with GC::Profiler
  • ✓Testing GC environment variables on your own Ruby version
  • ✓Writing finalizers that do not hold a reference to self
  • ✓Using WeakRef for references the GC does not count
Free preview · Lesson 1 3 min read

Counting live objects with ObjectSpace

The lesson "Memory management", in Operating system basics, covered the heap-of-slots model, mark and sweep, generational GC and GC.stat's heap_allocated_pages/heap_live_slots/count fields. It also covered find_each and frozen string literals as memory patterns in Rails. None of that is repeated…

Read the full lesson free →

Curriculum

7 lessons
Type to start searching...