r/ProgrammingLanguages • u/funcieq • May 06 '26
Discussion The ARC vs GC Debate
Hi!
I've been creating my own programming language for a few months now, and every time I mention it, I get the same question... What memory model do you have? And of course I have an ARC + cycle collector (python has the same), And usually... People don't like it, I wonder why and I've never gotten a detailed answer. So, I would like to know what YOU think about ARC, and what you think about GC. And why do you prefer one over the other?
36
Upvotes
36
u/mamcx May 06 '26
"People don't like it" apply to any decision take in language design, but sometimes is a reflection of the poor synergy of the different features of a language. Also, could be there is something off about it but the person not quite know what and pick anything as target.
16
u/RedCrafter_LP May 06 '26
I'm going with the guess that you mean atomic reference counting with arc as no other abbreviation in the context comes to mind.
I guess people that decide on gc favor comfort in use and straightforward collection algorithms. In which case an arc that does its own independent cleanup attempts outside the gc cycle complicates things.
Meanwhile people favoring arc heap management favor predictability and cost effective cleanup sacrificing cycling memory as a casualty or dealing with it by facilitating the language user with methods to avoid or break cycles. When you are deciding for arc you don't want a collector sweeping every once in a while breaking your predictable stable deallocation strategy just to free a few killobytes worth of arc reference loops.
It's just both strategies have upsides and downsides that are in many ways opposite and you add some of upsides by combining while piling up all the downsides of both giving a new loss to the hybrid approach.
19
u/waterlens May 06 '26 edited May 06 '26
Reference counting is often implemented badly than a tracing GC counterpart and struggles with cycles. However, if we could have a backup cycle collector or in a non-cycle background (like pure language that can not make cycles through mutation), reference counting can be really competitive.
For example, https://arxiv.org/abs/2210.17175 is a SOTA of GC research that partially uses RC. Another example is Koka and Lean4, both of which eliminate the extraneous incRef and decRef along with a reuse mechanism dedicated to reference counting. By the way, they also use mimalloc, an allocator with deferred release and thread-local allocation.
Bad implementations of RC often ignore these aspects, for instance, use only atomic operation to maintain the RC instead of a biased RC; cannot eliminate a incRef followed immeidately by a decRef; or cannot correctly exploit the lifetime of objects, leading to useless incRef & decRef pair when entering and leaving functions where objects are passed in arguments.
There is duality between a tracing GC and RC (https://dl.acm.org/doi/10.1145/1035292.1028982). It implies that every good GC can have an optimization that has a counterpart in RC, and vice versa. However, after the folklore for existing bad designs (like shared_ptr) has been widely spread over the community, less people are devoting to a good RC implementation, causing a misconception that GC is superior over RC.
17
u/SwedishFindecanor May 06 '26 edited May 06 '26
One big difference between languages with automatic storage reclamation is between those that have immediate reclamation (i.e. as early as possible), and those that have deferred reclamation (to be completed eventually). Languages with the former can have "destructors", that get called in a deterministic order. Languages with the latter can have "finalizers" that get called whenever in any order.
You would have to choose which type of language you want yours to be. Reference counting can be either immediate or deferred. Tracing GC only deferred. (There do exist a couple more GC algorithms with immediate reclamation but all I know are slower and require more memory than RC)
For deferred reclamation, there is more like a continuum of different algorithms using aspects of RC and tracing GC with different trade-offs than two distinct camps.
There are also some languages that use both RC and tracing GC for different things: For example: Pony uses tracing GC within each thread, and atomic RC between them.
RC has a few interesting properties that tracing GC algorithms do not:
A few thoughts:
22
u/WittyStick May 06 '26 edited May 06 '26
Atomic reference counting is very difficult to get right due of the ABA problem when it comes to releasing resources. Many implementations are flawed.
The basic problem is that when the refcount is decremented to zero, this doesn't necessarily mean it is safe to release the resource. Between decrement/test for zero and actually releasing the resource - another thread may subsequently increment the refcount (from zero) before the reclamation has occurred, the reclaiming thread could resume and release a resource that is still required by the other thread.
The C
<stdatomic.h>and C++<atomic>libraries don't even provide the necessary primitives to implement ARC correctly, as they're missing "primitives" likeatomic_dec_if_not_one/atomic_inc_if_not_zeroandatomic_dec_and_testwhich we need to do it correctly. (See the Linux kernelrefcount.h/refcount.cfor an example of correct implementation).Some of these "primitives" aren't actually primitive in hardware - they can be implemented for example using
cmpxchgin a loop - which is not cheap, and it's questionable whether overall performance is any better as a result. The performance impact of atomics themselves shouldn't be underestimated either - each atomicinc,decorcmpxchgmust bypass one or more cache layers if the data may be accessed by more than one CPU core - and the additional latencies to access higher cache levels can stack up. GC may be more cache-friendly.The main performance advantage of ARC is that it is more predictable - no stop-the-world pauses - but the overheads are more evenly distributed through the running time of a program - however, there are also GC designs which avoid such pauses.
ARC is simpler and more efficient to implement when we delay reclamation of resources like GC would - by using hazard pointers. We still need a GC-like mechanism to collect the hazard pointers though - so these are a kind of hybrid approach to memory management.
But one of the main advantages of ARC over GC is that we immediately free up a resource when it is no longer needed - as that resource may be required elsewhere. Delaying reclamation using GC or hazard pointers is what we would want to avoid in those cases, though they are few.
I would consider that as the defining aspect of whether you should use a pure ARC solution or not. If the resource needs to be released immediately, use ARC - if the release of the resource being delayed is not an issue, use GC or (A)RC+hazard pointers.
6
u/coderpants May 06 '26
Focus on what brings you joy, first and foremost. You're likely writing your own language to scratch some form of itch, and if having a working reference counting model allows you to focus on the things that lets you scratch that itch, then go for it.
The language I'm writing at the moment uses reference counting for a couple of reasons:
* I really like deterministic behaviour of destructors, particularly since otherwise you typically do various workarounds in the form of finalisers, defer, etc
* I want easy interoperability with C++ - my language transpiles references to shared_ptr<>, sum types to variant<>, etc - so interconnecting with native code is "trivial" (for certain values of trivial)
5
u/Adventurous-Trifle98 May 06 '26
I think the choice of memory management model should be based on the specifics of the language. Maybe even the other way around, that the language should be designed around how memory is managed.
Some programming languages encourage or enforce tree structured data while others use more graph based data. If the data is strictly tree structured, then there is no need for cycle collection.
Another reason to choose reference counting over tracing garbage collector is for simpler interoperability with C.
4
u/nick_ May 07 '26
Why not both! You could statically determine if a data structure possibly contains cycles, and have those use GC. For all non-cyclic data structures, ARC can be used.
3
u/csb06 bluebird May 08 '26
Here is a transcript of an interview where Chris Lattner (creator of LLVM who also worked on Apple's Objective-C and Swift compilers that implement ARC) on why he thinks ARC is superior.
3
u/torp_fan May 10 '26 edited May 10 '26
There's no mention of Nim in any of the other comments but it's worth looking at ... it gives the user a choice:
Introduction to ARC/ORC in Nim - Nim Blog
Nim's Memory Management
as an aside on Nimony aka Nim 3: can somebody provide a reference explaining/dem... | Hacker News
4
u/Ok-Scheme-913 May 07 '26
Sorry I have to.. reference counting is a GC. What you are thinking of comparing with is a tracing garbage collector.
As for my take on it: atomics are slow. Like probably the most expensive instruction on a modern CPU that has to clear caches etc.
If you do an atomic instruction on touching every object, you would get very significant overhead, without an optimizer that can remove some of these cases (e.g. escape analysis).
So with the added complexity of good compiler passes AND a tracing GC either way to find cycles, I honestly don't like this option all that much. It makes sense for Rust where it is only used for very few objects (and has a non-atomic version for single threaded code), or swift where they can do the optimizer passes to remove/convert to non-atomic in many cases (plus they want to target mobile).
But yeah, for a hobby language do whatever you want. For a "prod" language I do like tracing GCs, they have a very bad name, undeservedly.
2
u/Smallpaul May 06 '26 edited May 07 '26
IMO There is no debate between the two. They are just engineering tradeoffs. GC deals better with cyclic data structures. ARC (if implemented well) has better performance.
Edit: okay by “better” performance I meant “more predictable performance and more predictable rules for when objects are cleaned up.”
1
u/Markus_included May 07 '26
It's probably because people think it's redudant to have both RC and tracing GC, although if you use the same cycle collection algorithm as PHP (Bacon-Rajan) it's pretty nice and not as redundant as what python does (micropython only has tracing GC and no RC for this reason). Another cool optimization for RC is that you can consolidate refcount updates using static analysis which dramatically reduces cost for e.g. retaining and releasing in a loop or within the same scope. I find it pretty cool
1
u/david-1-1 May 08 '26
Reference counting and cycle prevention is a great GC method when the language or software supports it, because it is efficient and avoids moments of compute-bound spinning when switching generations. But not all languages support it.
1
u/Guvante May 07 '26
IMHO given how much is known about GC you should be using one unless your niche doesn't support them.
Sure there are downsides and technically you can do anything with other methods but the upsides are way more impactful.