Jon Flow Jon Flow - 1 year ago 48
Swift Question

What's the difference between atomic reference counting and automatic reference counting?

I see ARC as an acronym used every now and then, but for example in Rust it's "atomic reference counting", and in Swift or ObjC they call it "automatic reference counting". What exactly is the difference? As far as I know they seem to behave the same way.

Answer Source

This is actually... totally different.

  • atomic: comes from atomicity, it describes an operation that is executed in one step, that is an external observer cannot see any intermediate step: she only sees the state before or after
  • automatic: means done or produced as if by a machine

On the Rust side, we have Arc and Rc which are reference counting pointers:

  • Rc is the bare-bone reference-counting pointer: cloning it just increments a counter, and when the Rc pointing to a particular piece of data is dropped, the counter falls down to 0 and that piece of data is dropped too
  • Arc is the atomic version of it, which means it is thread-safe and can be shared across threads (unlike Rc)

On the Swift side, ARC refers to Automatic Reference Counting, Swift's Garbage Collection mechanism which as the name implies is automatic (and invisible in the code).