logoalt Hacker News

pjmlp • today at 6:53 AM • 4 replies • view on HN

Reference counting is a GC algorithm, and no this isn't expected, it depends pretty much on the implementation.

Many make the mistake to think there is only one way to do a GC.

One of the authoritative books on the subject, https://gchandbook.org/contents.html

And a quite well known paper on the matter as well, https://dl.acm.org/doi/10.1145/1035292.1028982


Replies

adrian_b • today at 8:34 AM

Whether reference counting is a GC algorithm depends on how you define what GC is.

I prefer to consider GC only the methods of memory management where reclaiming the no longer used memory is done either asynchronously with the main program or as late as possible, i.e. when new allocation requests cannot be satisfied.

In the normal implementation of reference counting, memory is freed as soon as possible, i.e. exactly like stack memory, when blocks are exited, so I do not consider reference counting as GC.

The problem with GC in the strict sense is that you cannot predict when it will happen. With both stack memory and reference counted heap memory you know that whenever you exit a block, some time will be spent with running destructors and for freeing memory, but such interruptions will not happen in other points of the program.

➕ show 3 replies
red75prime • today at 7:50 AM

You just need to remember that garbage (unreachable reference cycles) isn't collected.

IshKebab • today at 7:40 AM

In common use, GC refers to mark and sweep and similar systems where allocations are not freed at an easy to predict time.

➕ show 1 reply