seniorRuby

How does Ruby manage heap fragmentation and compaction in GC?

Updated May 17, 2026

Short answer

Ruby GC reduces fragmentation using slot reuse and optional heap compaction strategies.

Deep explanation

CRuby allocates objects in fixed-size slots, which reduces external fragmentation. However, internal fragmentation can occur due to uneven object lifetimes. GC reuses freed slots via free lists. Modern Ruby also supports heap compaction, which moves objects to eliminate gaps and improve memory locality, at the cost of pointer updates.

Unlock with a Pro subscription to view this section.

View pricing

Real-world example

No real-world example available yet.

Unlock with a Pro subscription to view this section.

Upgrade to Pro

Common mistakes

No common mistakes listed yet.

Unlock with a Pro subscription to view this section.

Upgrade to Pro

Follow-up questions

No follow-up questions available yet.

Unlock with a Pro subscription to view this section.

Upgrade to Pro

More Ruby interview questions

View all →