开发者

Objective-C Garbage Collection - What does it do?

开发者 https://www.devze.com 2023-03-17 08:04 出处:网络
Being a python programmer for four years now (it doesn\'t mean much though) and moving to Objective-C the one concept that is weird and \"alien\" to me is memory management. Luckily garbage collectiom

Being a python programmer for four years now (it doesn't mean much though) and moving to Objective-C the one concept that is weird and "alien" to me is memory management. Luckily garbage collectiom exists and I only intend to develop apps for the mac OS 10.6+, so in all my projects so far I have always turned garbage collection to required. But here is my problem: when I use Instruments with the Alloc and Leaks tool I see leaked bytes poppin in the graph. ??? Very weird. What does Garbage Collection really do, when it is required. The way I see it is that you can completely forget about retain, release, etc. But is that true? Please provide examples where GC will help and where it won't (if any), so that I can understand what I am doing wrong.

Edit

I probably should have 开发者_运维百科been more clear. The problem I want to solve is the fact that, even after GC is set up as required, Instruments still finds leaks. I also asked what GC does just so I could make sure that it does what I think, and the problem is not in my code or in GC, but elsewhere. That "elsewhere" is what I want to find out.


If you have time to wait for the next version of Xcode to be released (together with Lion), you can directly move to ARC (Automatic Reference Counting).

ARC will - in the intermediate run - replace GC in OS X and iOS.

If you are a registered Apple Developer, you can check out presentations on ARC in the WWDC2011 videos.


A simple answer your question is, when you have variables or objects that are allocated to memory, GC cleans up the mess for you. Whereas; in iPhone and iPad applications, you must clean it up yourself because GC doesn't exist.

Example:

NSArray* arr = [[NSArray alloc] init]; // Allocated to memory
[arr release]; // released from memory
0

精彩评论

暂无评论...
验证码 换一张
取 消