Ruby จัดการหน่วยความจำ โดยอัตโนมัติ ผ่าน garbage collector (GC) ที่เรียกคืนอ็อบเจกต์ที่ไม่ถูกอ้างอิงอีกต่อไป Ruby สมัยใหม่ (MRI) ใช้ตัวเก็บขยะแบบ generational, incremental mark-and-sweep พร้อมการ compaction การเข้าใจมันช่วยในเรื่องประสิทธิภาพและการวินิจฉัยปัญหาหน่วยความจำในแอปพลิเคชันที่ทำงานเป็นเวลานาน
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
