Autorelease iPhone

前端 未结 6 554
遥遥无期
遥遥无期 2021-02-02 04:29

Coming up towards the end of developing an iPhone application and I\'m wondering just how bad is it to use autorelease when developing for the iphone. I\'m faced with some fairl

相关标签:
6条回答
  • 2021-02-02 05:11

    For iPhone performance reasons, Apple suggest that, whenever possible, you shouldn't use autoreleased objects. Instead, explicitly release your objects when you are done with them.

    0 讨论(0)
  • 2021-02-02 05:20

    More important than the autorelease or manual-release choice is how often you alloc and dealloc your NSAutoreleasePools. Since most of the Cocoa frameworks use autorelease liberally, you need to have a proper pool draining strategy. Once that is in place, the choice of whether to release or autorelease becomes much less an issue.

    That being said, the only areas you should worry about are tight loops--allocate and release an NSAutoreleasePool every few iterations for best results; and when you have spawned another NSThread that doesn't have a Runloop--create a pool and drain it every so often becomes idle. Since most applications only allocate a small amount of data per event, UIKit's strategy of allocating the pool before the event is dispatched and releasing it after the dispatch returns works very well.

    0 讨论(0)
  • 2021-02-02 05:23

    Using release instead of autorelease can improve memory usage in tight spots (which is good on the iPhone), but it's not going to help at all with crashing if you're not following the retain / release rules. I would read a few tutorials on memory management in Obj-C if you're still a little hazy on what you should be doing, and then go after those crashes using the debugger and crash reports to find out where you're over releasing objects. This and this are two good places to start.

    0 讨论(0)
  • 2021-02-02 05:23

    When you autorelease, you're basically saying: "I don't need this any longer, but anyone else is free to pick it up (before the auto release pool is drained)". When you explicitly relase an object you're saying: "I don't need this any longer and unless anyone else has already said otherwise (acquired), it should be deallocated immediately."

    Consequently, autorelease is not normally the wrong thing to. It is required when you want to pass objects back to the sender of a message without requiring the sender to take care of releasing the object.

    0 讨论(0)
  • 2021-02-02 05:28

    Using autorelease pools means that you might be leaving some unused memory lying around. Since the iPhone has less memory to go around, you might improve performance if you free up unneeded memory as soon as possible, rather than letting it sit around taking up resources while it waits for an autorelease.

    0 讨论(0)
  • 2021-02-02 05:29

    If you think you dunno how to use autorelease, check out CS193p FALL 2010 on iTunes U -> Lecture number 4.

    It teaches you all about memory management and stuff (if you skip the first 10 minutes or so)

    0 讨论(0)
提交回复
热议问题