IPhone Memory Management didReceiveMemoryWarning

iphonememory-management

Ok……

I'm implementing a simple OpenGL ES application on the iPhone and I recently added in Pinch Media Analytics. Doing this helped uncover a memory management problem, and I'm not entirely sure how to deal with it.

In a perfect world, my application – which loads PNGs and .CAF files in didFinishLoading will launch, load all of it's resources, and run just fine.

HOWEVER, if my program gets a crash (which happened as I was integrating the Pinch Media libraries) or if I run Safari and open a bunch of pages and then launch my game, the game will crash back to the menu because it is out of memory.

This problem will persist until I do a hard reset of the system.

The default answer that you sort of get online is to implement the didReceiveMemoryWarning method as listed below….

- (void)didReceiveMemoryWarning
{ 
  // default behavior is to release the view if it doesn't have a superview.

  // remember to clean up anything outside of this view's scope, such as
  // data cached in the class instance and other global data.
  [super didReceiveMemoryWarning];
}

But this doesn't really help as it's the other programs that are holding onto memory not mine. I don't want to release my own view do I? Is there a good description of how to handle this situation and / or what happens in the didReceiveMemoryWarning event?

Best Answer

Welcome to the shared memory pool with no VM.... There's not a lot you can do here, but there are a few things (and it's possible it's actually your fault and you can completely fix it). Game developers often recommend their customers reboot before running them for this reason, so you may need to be in the same boat if you really need a lot of memory to be effective.

Of course you should try to minimize your own memory footprint. But you should also try to avoid excessive memory fragmentation. Sometimes the problem isn't that there's no memory; there's just no blocks large enough. Sometimes it's better to use a Mutable and keep modifying it rather than generating a new immutable object. This is particularly true of large NSStrings, which can really trash memory.

Keep in mind that UIImage +imageNamed: will keep the image around after you release it, so if you don't need them any more, you need to clear those out. Set its name to nil before releasing it to stop it caching.

Make sure to run your app under Instruments. You may be eating more memory than you think you are.

Don't forget the autorelease pool. If you generate a lot of autoreleased objects in a single event loop, you may need to drain your pool periodically so your don't spike memory. Memory spikes can lead to a program with modest memory requirements suddenly getting killed.