Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C 2.0 Garbage Collector VS Automatic Reference Counter in IOS 5 SDK

Just wondering if anyone knows what is the different between Objective C 2.0 Garbage Collector and new Automatic Reference Counter in IOS 5 SDK ?

is IOS 5 SDK also use Objective C 2.0 ?

note : what I mean objective C 2.0 - I saw from this link http://theocacao.com/document.page/510

Thanks

like image 370
kkurni Avatar asked Oct 26 '11 08:10

kkurni


People also ask

What is the difference between arc automatic reference counting and GC garbage collection?

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.

Does Objective-C use garbage collector?

Objective-C 2.0 adds garbage collection to the language as a replacement to the traditional retain / release reference counting scheme. Garbage collection is automatic object memory management.

Is reference counting garbage collection?

Reference counting. Reference counting garbage collection is where each object has a count of the number of references to it. Garbage is identified by having a reference count of zero. An object's reference count is incremented when a reference to it is created, and decremented when a reference is destroyed.

What is ARC in Obj C?

Automatic Reference Counting (ARC) is a memory management option for Objective-C provided by the Clang compiler. When compiling Objective-C code with ARC enabled, the compiler will effectively retain, release, or autorelease where appropriate to ensure the object's lifetime extends through, at least, its last use.


2 Answers

Just wondering if anyone knows what is the different between Objective C 2.0 Garbage Collector and new Automatic Reference Counter in IOS 5 SDK ?

ARC is not a Garbage Collector. It is better to think of it as manual reference counting (retain/release/autorelease) calls which are added by the compiler. It also uses some runtime tricks.

If you are completely new to ObjC on Apple systems: All of Apple's Objective-C types use reference counting, but there are multiple variants now. Before ARC, and before GC, all we used was manual reference counting (MRC). With MRC, you would explicitly retain and release your objects. MRC was difficult for some people, particularly those who had spent little time managing their memory explicitly. Therefore, the demand for simpler systems grew over time. MRC programs also require that you write a good amount of memory management code, which can become tedious.

See Brad's excellent answer here for some more details.

is IOS 5 SDK also use Objective C 2.0?

Yes, but the ObjC Garbage Collector is not and was never an option on iOS.

like image 80
justin Avatar answered Nov 15 '22 22:11

justin


NB: Garbage collection isn't available on iOS but according to my comments, ARC is available on Mac OSX 10.6+. The differences are still comparable however.

With automatic reference counting, objects are still deallocated as soon as they go out of scope.

With garbage collection, objects can remain in memory until the garbage collector does its next sweep and finds objects which no longer have references.

like image 26
James Webster Avatar answered Nov 15 '22 23:11

James Webster