Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best approach to debugging applicationDidReceiveMemoryWarning on iPhone?

Need advice on how to debug this. I'm new with limited environments and have no previous embedded or smart phone programming experience so I could use some clues. Already aware of: Instruments, Clanger Static Analysis, manual code review, etc. Instruments seems to be very helpful in general but quite time consuming and freezes up a lot of the time! Clanger has also helped me a lot as well. It seems like I'm just consuming too much memory in general and I'm wondering what a good strategy is. Do I release some top-level objects? Is there a 'preferred strategy'?

Just wondering if anyone has tackled this successfully and if they have any other suggestions? Thanks all.

like image 417
Rob Avatar asked Mar 20 '09 16:03

Rob


2 Answers

There are a lot of good articles for memory management in an iPhone app. Here are some useful links.

  • http://iosdevelopertips.com/objective-c/memory-management.html
  • http://kosmaczewski.net/2009/01/28/10-iphone-memory-management-tips/
  • https://cocoa-touch.blogspot.com/2008/09/memory-management-on-iphone.html

Things you should in general take care of

  1. Release any variables which you do not need
  2. Always handle didReceiveMemoryWarning and release any variables not in use
  3. Stop any memory-heavy processes in applicationDidReceiveMemoryWarning like audio/video playing, UIImagePickerController etc

EDIT This does not apply any more. imageNamed: had caching issues prior to 3.x OS versions. The issue does not exist any more and you should use imageNamed: (makes implementing retina display easier)

  1. Do NOT use imageNamed: to create UIImage objects.
like image 62
lostInTransit Avatar answered Oct 20 '22 00:10

lostInTransit


Basically you're receiving this warning because (unsurprisingly) the iPhone is dangerously low on memory. This can generally be for one of two reasons;

  1. You have a memory leak.
  2. You are allocating far too many objects and need to revisit your design.

For the first one you should run instruments and examine your memory allocations. This can really slow down your app (and requires additional memory) so try testing areas of your app one at a time. E.g. if you have multiple views switch between them a couple of times.

For the second you will have to examine things you are doing that could result in large memory allocations. For example if you're writing a Flickr browser you might need to cut down the number of images you have loaded at anyone time, or free up some unused ones when you receive this warning.

These are about the only general rules I can suggest without knowing more about your app.

Unfortunately there's no real way (that I know of) to get figures for current memory allocation from the iPhone OS. This makes it really difficult to isolate the areas of your application that are inadvertently memory hungry.

like image 43
Andrew Grant Avatar answered Oct 20 '22 00:10

Andrew Grant