Mustafa
Mustafa

Reputation: 20564

Is garbage collection supported for iPhone applications?

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.

Upvotes: 12

Views: 12355

Answers (5)

Stephen Darlington
Stephen Darlington

Reputation: 52565

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

Upvotes: 7

Dhiraj
Dhiraj

Reputation: 350

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.

Upvotes: 1

DD.
DD.

Reputation: 21991

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

Upvotes: 2

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

Upvotes: 2

adam
adam

Reputation: 22587

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!

Upvotes: 32

Related Questions