Ruby beheert geheugen automatisch via een garbage collector (GC) die objecten zonder verwijzingen vrijgeeft. Modern Ruby (MRI) gebruikt een generationele, incrementele mark-and-sweep collector met compaction. Dit begrijpen helpt met prestaties en het diagnosticeren van geheugenissues in langlopende applicaties.
Automatische garbage collection
Ruby allocates objects on the heap; the GC reclaims those that are no longer REACHABLE
(referenced). You never free memory manually.
Modern MRI GC characteristics:
✓ Mark-and-sweep — marks reachable objects (from roots), sweeps unreachable ones
✓ Generational (since Ruby 2.1) — new objects collected often, old ones rarely
(based on "most objects die young")
✓ Incremental — spreads GC work to reduce pause times
✓ Compacting (GC.compact, Ruby 2.7+) — reduces memory fragmentation
