Is there any garbage collector concept available in iOS?if yes please guide us how and it's Recommended or not?
-
possible duplicate of [Is garbage collection supported for iPhone applications?](http://stackoverflow.com/questions/416108/is-garbage-collection-supported-for-iphone-applications) – p.campbell Jun 22 '12 at 19:59
-
What would you like guidance about? Are you asking whether there is a way to explicitly invoke the garbage collector? – anonuser0428 Jun 22 '12 at 20:00
-
@p.campbell That dupe is pretty old and the answer has changed. The answers to the Q you linked are all of the "use retain/release" variety, but the preferred answer these days is to use ARC. – Caleb Jun 22 '12 at 20:03
-
Caleb: isn't that exactly how SO works, though? Questions and answers should be living and breathing as the world changes. An answer should be placed there with "in iOS 5, you can do xyz", etc. This question could be asked 50 times for each iOS version. We should have one canonical question, and the rest closed as dupes. – p.campbell Jun 22 '12 at 20:06
2 Answers
There's no garbage collector under iOS. Instead, simply use automatic reference counting (ARC). ARC will take care of most of the memory management for you without the runtime overhead of a garbage collector.
Note that although MacOS X does support garbage collection, GC will be deprecated in favor of ARC in MacOS X 10.8. So ARC is now the preferred solution even on the desktop.

- 124,013
- 19
- 183
- 272
What would you like guidance about? Are you asking whether there is a way to explicitly invoke the garbage collector? because as far as I know iOS does not have garbage collection (look at ARC automatic reference counting). And even while working with a language like say java which does have GC, the user cannot invoke GC, we can only request for GC and the system will oblige but we don;t know when exactly the Garbage collection will take place.
Garbage collection is usually a process invoked by the system and not invoked by the user and since this garbage collection 'thread'(usually called the daemon thread as it has lowest priority) is invoked as needed by the system and it might thus be invoked multiple times during the lifecycle of an application, if it were present in iOS, it would lead to a very bad battery life for the iphone as GC would sap the battery life and hence we use alloc and release commands while working with iOS applications.

- 11,789
- 22
- 63
- 86