12

Does the iPhone support garbage collection? If it does, then what are the alternate ways to perform the operations that are performaed using +alloc and -init combination:

NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:xmlData];
UIImage *originalImage = [[UIImage alloc] initWithData:data];
detailViewController = [[[DetailViewController alloc] initWithNibName:@"DetailView bundle:[NSBundle mainBundle]] autorelease];

... and other commands. Thank you in advance for any help or direction that you can provide.

adam
  • 22,404
  • 20
  • 87
  • 119
Mustafa
  • 20,504
  • 42
  • 146
  • 209

5 Answers5

32

No. Garbage collection is too large an overhead for the limited battery life etc. on the device.

You must program always with an alloc/release pattern in mind.

NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:xmlData];
...
[xmlParser release];

or (not suitable for every situation)

NSXMLParser *xmlParser [[[NSXMLParser alloc] initWithData:xmlData] autorelease];

Hope this helps!

adam
  • 22,404
  • 20
  • 87
  • 119
  • 3
    If battery life is one of the reasons that the iPhone does not have GC, why does Android have a Garbage Collector? See: http://developer.android.com/reference/java/lang/System.html#gc%28%29. Would you mind passing along a reference to the source that states that battery life is one of the reasons why the iPhone does not have GC? Thanks. – Tim Stewart Jun 30 '09 at 13:59
  • 8
    iPhone OS made by Apple, Android OS made by Google. It is common sense a continually running garbage collection process will consume extra processing power, drawing more power from the battery. All of the iPhone choices made to conserve battery power were ignored in Android, and as a result Android phones have worse battery life than iPhone. – adam Dec 10 '09 at 15:57
  • Like adam said, most Android phones have shit battery life. My friend has a Droid, and he can't barely do anything on it without running down the battery obscenely. – Jonathan Sterling May 07 '10 at 14:46
  • So as the iPhone gets more powerful with more memory and stronger battery life, do you think garbage collection will be put back in? – Jack Marchetti Dec 08 '10 at 22:03
  • Incredibly unlikely; as all good iOS programmers know their way around the Cocoa memory management model, it will always be an unnecessary overhead. – adam Dec 30 '10 at 12:50
  • 8
    I think that Apple’s argument about battery life is weak at best. Sure, GC is an overhead, but it is nothing compared to the billion other things running on the phone anyway. And while it is true that all good iOS programmers know their way around the Cocoa memory management model, the reality is that far from all iOS programmers are in fact good. And without a safety net like GC we end up with tons of shitty apps crashing all the time which the App Store is littered with. So IMHO Apple should have included GC for the sake of their users, esp. with CPU and battery life improving all the time. – pajevic Apr 28 '11 at 09:40
  • iOS5 will keep track of every object and release/retain will no longer be API methods. – adam Jul 13 '11 at 09:03
7

No, garbage collection is not supported on the iPhone currently. You need to use alloc/release/autorelease.

sblom
  • 26,911
  • 4
  • 71
  • 95
Stephen Darlington
  • 51,577
  • 12
  • 107
  • 152
2

Mono touch has garbage collection and runs on the iPhone os.

DD.
  • 21,498
  • 52
  • 157
  • 246
  • Mono touch apps are written in C# and eventually compile down to Objective-C. At some point during the special compilation the memory management for objects is inserted. I assume. I can only speculate, but I would go on record saying that this probably isn't great for memory efficiency. – Jasarien Sep 16 '10 at 16:07
  • Worth remembering this overheard will be no worse than a c#, or other memory-managed language (java etc). It has it's purposes, is the point I'm trying to make :) – Dave Jun 07 '11 at 15:12
2

Note the lack of garbage collection means weak references are not supported either.

Kendall Helmstetter Gelner
  • 74,769
  • 26
  • 128
  • 150
  • Would you mind giving an example of what we're missing without weak references (or a link, at least)? – Dan Rosenstark May 12 '10 at 11:24
  • 2
    This question is a good example: http://stackoverflow.com/questions/735551/weak-keyed-dictionary-in-objective-c Imagine a hash map, where anything it held could be dealloced and then forgotten - so you wouldn't be over-retaining references, but would not have to worry about invalid pointers stored in a dictionary either. – Kendall Helmstetter Gelner May 12 '10 at 18:41
1

In the entire discussion nobody says about the Java language, in Java the Garbage collection is in-built in the language so it is implicitly available in Android,J2ME and Blackberry :), where as in Objective-C it is optional, so in iPhone the GC is not available.

Dhiraj
  • 350
  • 1
  • 3