views:

5351

answers:

7

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.

+4  A: 

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

Stephen Darlington
+23  A: 

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
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
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
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
A: 

Thanks for the help. Just realised that i did saw the answer some where, and now i know where:

Creating iPhone Application - No Garbage Collection feature

silly me : )

Mustafa
This should be a comment on the answer you are responding to, not an answer on its own.
Chris Hanson
+1  A: 

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

Kendall Helmstetter Gelner
Would you mind giving an example of what we're missing without weak references (or a link, at least)?
Yar
This question is a good example: http://stackoverflow.com/questions/735551/weak-keyed-dictionary-in-objective-cImagine 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
A: 

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

I disagree. If this is true then how come GC is implemented in Java Micro Edition which targets wireless devices?

Kemal
Nobody said it can't be done. Apple choose to save battery life by not including it. That's all. The net result is a much better user experience than that provided by J2ME apps.
Adrian Kosmaczewski
A: 

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

DD
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
A: 

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