Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 602
Jon Flow Avatar asked Oct 09 '16 12:10

Jon Flow


People also ask

What is atomic reference counting?

The essence of atomic ref counting is to ensure that if two different instances of a shared_ptr (that are managing the same object) are accessed/modified, there will be no race condition. However, shared_ptr doesn't ensure thread safety, if two threads access the same shared_ptr object (and one of them is a write).

What is difference between manual and automatic reference counting?

Without ARC or GC, you would need to manually add calls to retain and release, which would be Manual Reference Counting (MRC). In the case of the latter, human beings add the calls in the source code at development time, not at runtime or compile time.

What is the difference between ARC automatic reference counting and GC garbage collection in Swift?

ARC differs from tracing garbage collection in that there is no background process that deallocates the objects asynchronously at runtime. Unlike tracing garbage collection, ARC does not handle reference cycles automatically.

What is automatic reference counting in IOS?

Swift uses Automatic Reference Counting (ARC) to track and manage your app's memory usage. In most cases, this means that memory management “just works” in Swift, and you don't need to think about memory management yourself.


1 Answers

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: they only see 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 last 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).

like image 141
Matthieu M. Avatar answered Nov 15 '22 08:11

Matthieu M.