views:

427

answers:

2

I was looking for a book, text, post or something talking about iPhone optimization but I couldn't find anything except for Apple documentation.

I think it would be great to talk about which are the common steps you do when optimizing your apps performance. I'm now trying to improve my app memory usage and I've figured out it is really difficult when your app is complex.

How do you handle view creation and destruction? Are you doing it by yourself or do you delegate on iPhone navigation controller?

Do you use any trick to free some memory in your app?

+2  A: 

To optimize the memory usage, avoid using auto-released objects. If you don't need an object anymore, release it explicitly, so the memory will be reclaimed immediately. Otherwise, the object will stay in the pool for an unknown time.

Note that this will optimize your application's memory usage, but not the performances. Releasing explicitly objects may slow down your application. You'll sometimes have to decide if you want a faster application, or an application that uses less memory.

Also try to use C code whenever possible. For instance, if you just need to store objects in an array, don't use a NSMutableArray unless you rely on a particular method. If not, use 'id *' variable, and use malloc() and free().

Hope this helps a bit... : )

Macmade
+1  A: 

If you have a loop which might be executed many times over, it might be worth it to manually manage the NSAutoreleasePool in that case. More often than not you end up with a lot of auto-released objects (remember Foundation etc. generate auto-released objects, too) and waiting for your thread to end or control to return to the run loop could cause memory problems and slow your application. There is no guaranteed recipe for success here, so you might have to probe a bit.

One of the huge memory eaters are images, and everything that has to do with them. If you have a UITableView with hundreds of rows and each cell containing an image, you might not want to keep them all around in memory. You could keep a cache of those images in the file system and load the images as needed. Disk I/O might be relatively expensive, but if you watch the memory notifications closely and read ahead wisely, you'll hardly notice a performance impact.

Key techniques to remember when trying to reduce your app's memory usage are using the static analyzer (Cmd+Shift+A) and Instruments, especially the object allocation instrument.

One thing I try to force myself to do is avoiding instance variables whenever possible. They make your code harder to maintain and consume memory because they're usually not deallocated until their owner. Try to avoid unnecessary instance variables, or try to release them early if they can be recreated (from disk, for example).

Alexander Repty