How does Swift's ARC avoid a fragmented heap?

One of the positive aspects of a tracing garbage collector is that the heap can be compacted during GC pauses, reducing fragmentation.

Since Swift obviously doesn't have GC pauses, is there any system to fight heap fragmentation?

Not really, no. The underlying system memory allocator probably has some features intended to minimise fragmentation (e.g. separate pools for common small allocation sizes), and things like Array will scale their storage in a sensible manner, but there’s nothing in the Swift runtime that specifically attempts to avoid fragmentation.

3 Likes

Right, it’s just a downside of all non-copying collectors that they don’t magically compact the heap.

2 Likes