logoalt Hacker News

pebal • today at 9:02 AM • 1 reply • view on HN

> The problem with GC in the strict sense is that you cannot predict when it will happen.

It’s the same with ARC. You also don’t know when the counter will reach zero.


Replies

adrian_b • today at 9:41 AM

No, that is not true.

In the normal implementation of reference counts, counters can be decremented only at block exits and not at any other program point.

At a block exit some of the local variables that are freed may contain references, so freeing them will decrement some reference counts. Then some counters will reach zero, triggering other deallocations and the decrementing of other counters. This will repeat until no other counters reach zero.

All the memory deallocation happens predictably, only at block exits.

If a variable is not freed immediately when a counter reaches zero, but the deallocation is deferred for a later time, which is not predictable, that is no longer classic memory management with reference counts, but it is a garbage collector, which happens to also use reference counts, probably in combination with some tracing algorithm.

When reference counts are implemented, manual memory deallocation, like with C free() or C++ delete, should be forbidden, but even if it were used that would just introduce other program points besides the block exits, where it is known that memory deallocation will happen.

➕ show 1 reply