Ruby: memory & GC mastery
Ruby's garbage collector is generational, and this course explains what it decides and why. It covers minor and major collections, promotion after three survivals, the write barrier and the remembered set. It shows how to find what triggers a major collection with GC.latest_gc_info, including the oldmalloc path, and how to reduce GC.stat to three ratios. It separates leaks from bloat, measures GC.compact and copy-on-write in forked workers, and weighs allocation changes and GC tuning against the defaults.
What you'll learn
- ✓How generational collection, promotion and the write barrier work
- ✓Finding the cause of a major collection with GC.latest_gc_info
- ✓Reading GC.stat as occupancy, major share and churn ratios
- ✓Telling a memory leak apart from bloat
- ✓When GC.compact helps and how copy-on-write sharing decays in forked workers
- ✓Measuring allocations and deciding whether to change GC settings
The pause you did not schedule
Your p50 is 40 ms. Your p99 is 900 ms. The slow requests aren't slower queries, a different endpoint or one bad customer. They're scattered across everything, and when you line them up against process uptime, they arrive in a rhythm. Something in the process stops the world every few seconds, and…
Read the full lesson free →Curriculum
7 lessonsThe pause you did not schedule
Major collections cause tail latency spikes, measured here at about four times the cost of a minor one.
Generational collection and the write barrier
How objects are promoted after three survivals, why minors skip old objects, and how the write barrier keeps that safe.
What triggers a major GC
Reading major_by and paired GC.stat counters to find which threshold, including oldmalloc, fires a major collection.
Reading GC.stat as a diagnosis
Reducing GC.stat to occupancy, major share and churn ratios, compared as deltas between two samples.
Bloat versus leak
Telling a leak from bloat by measuring heap_live_slots across repeated work, and how fragmentation pins pages.
Compaction, fragmentation and copy-on-write
What GC.compact recovers, why auto_compact is off by default, and how forked workers lose copy-on-write sharing.
Allocation, tuning and when to stop
Which allocation changes pay off, such as frozen string literals, and the two measured reasons to change GC settings.