Ruby આપોઆપ એક ગાર્બેજ કલેક્ટર (GC) દ્વારા મેમરીને સંચાલિત કરે છે જે હવે સંદર્ભિત નથી તેવા ઑબ્જેક્ટ્સને પુનરુદ્ધાર કરે છે. આધુનિક Ruby (MRI) એક જનરેશનલ, ઇનક્રિમેન્ટલ માર્ક-અને-સ્વીપ કલેક્ટર કમ્પેક્શન સાથે ઉપયોગ કરે છે. તેને સમજવું લાંબા સમય ધરી રહેલી એપ્લીકેશનમાં પર્ફોર્મન્સ અને મેમરી સમસ્યાઓને નિદાન કરવામાં મદદ કરે છે.
આપોઆપ ગાર્બેજ કલેક્શન
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
