Ruby memory को automatically एक garbage collector (GC) के माध्यम से manage करता है जो उन objects को पुनः प्राप्त करता है जिनका अब reference नहीं रहा। Modern Ruby (MRI) एक generational, incremental mark-and-sweep collector का उपयोग करता है जिसमें compaction होता है। इसे समझना performance और long-running applications में memory issues को diagnose करने में मदद करता है।
Automatic 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
