Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does iOS 5 have garbage collection?

Tags:

Do I no longer have to worry about memory management iOS 5 onwards? Also, will all programs written for iOS 4 and earlier versions have to be rewritten to allow iOS to manage the memory for you?

like image 284
NSExplorer Avatar asked Jul 04 '11 22:07

NSExplorer


People also ask

Does iOS have a garbage collector?

iOS has no method of Garbage Collection. Even so, Garbage Collection is entirely unnecessary (for all practical purposes) when ARC is used. ARC works its magic at compile time to do the reference counting for you thereby making it unnecessary (and actually non-allowed) to use any other sort of memory management.

Is there garbage collection in Swift?

Many programming languages use garbage collection to collect unused memory, whereas swift uses ARC. ARC is technically a form of Garbage collection.

Is ARC a garbage collector?

Automatic Reference Counting is technically a form of garbage collection. However, typically when one refers to a garbage collector, they mean a separate process or subsystem that runs in the background independent of your application. ARC, on the other hand, is fully part of your application code.

When was ARC introduced iOS?

Apple first introduced Automatic Reference Counting in Mac OS X Snow Leopard and iOS 4. A more mature version of ARC was made available in Mac OS X Lion and iOS 5. This was a very important change for the developer community.


1 Answers

You appear to be talking about Automatic Reference Counting, mentioned in other answers. ARC is a kind of GC in that it automates memory freeing, but has a number of differences from a good garbage collector.

Firstly, it's mainly a compiler technology. The compiler knows about Cocoa's reference-counting guidelines, so it inserts retains and releases where they should be according to the rules. It works just like if you'd written the retains and releases yourself — it simply inserts them for you. Normal garbage collectors keep track of your program's memory while it is running.

Second, since it is just like retain and release, it can't catch retain cycles (if Object A retains Object B and Object B retains Object A, and nothing else references either of them, they both become immortal). You need to take the same precautions to prevent them.

It also uses resources differently from an automatic garbage collector. The garbage collectors used with Objective-C have to scan for unreferenced memory and collect it — which is expensive, and can lead to "stuttering" on slower systems — but they only have to do this occasionally, and in theory can even fine-tune their collection cycles to match how a program actually uses its memory. In general, a GC program will use more memory than a non-GC program and will slow down significantly when the GC decides to collect. ARC, on the other hand, moves the "scanning" to compile-time and frees memory as soon as it's available, but it has to constantly update object reference counts instead of waiting for garbage to build up like a collector.

like image 59
Chuck Avatar answered Sep 19 '22 13:09

Chuck