How does Ruby ensure correctness when objects move during GC compaction?
Updated May 17, 2026
Short answer
Ruby updates all references to moved objects using forwarding pointers and full heap traversal updates.
Deep explanation
During compaction, objects are physically moved to eliminate fragmentation. Each moved object leaves a forwarding address so references can be updated. GC then scans root sets and heap references to rewrite pointers. This ensures no stale references remain. The process requires stop-the-world pauses for correctness, as concurrent mutation would invalidate pointer consistency.
Unlock with a Pro subscription to view this section.
View pricingReal-world example
No real-world example available yet.
Unlock with a Pro subscription to view this section.
Upgrade to ProCommon mistakes
No common mistakes listed yet.
Unlock with a Pro subscription to view this section.
Upgrade to ProFollow-up questions
No follow-up questions available yet.
Unlock with a Pro subscription to view this section.
Upgrade to Pro