Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 217
Mustafa Avatar asked Jan 06 '09 10:01

Mustafa


People also ask

Does iOS support garbage collection?

This is familiar behavior to programmers who have used languages like Java or Python, where memory management “just happens.” Objective-C garbage collection is opt-in. You can choose to use GC or continue to use reference counting. iOS does not currently support GC, so you must use reference counting there.

Did the application takes care of garbage collection?

Java applications obtain objects in memory as needed. It is the task of garbage collection (GC) in the Java virtual machine (JVM) to automatically determine what memory is no longer being used by a Java application and to recycle this memory for other uses.

How does garbage collection impact on application performance?

An application that spends 1% of its execution time on garbage collection will loose more than 20% throughput on a 32-processor system. If we increase the GC time to 2%, the overall throughput will drop by another 20%. Such is the impact of suspending 32 executing threads simultaneously!

Does Swift have garbage collection?

Swift does have garbage collection! It uses reference counting, and AFAIK it doesn't have cycle detection, which means it's up to the developer to ensure that no cycles occur.


1 Answers

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!

like image 164
adam Avatar answered Nov 15 '22 20:11

adam