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.
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.
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.
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!
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With