2762 Views 1 Reply Latest reply: Oct 23, 2008 8:55 PM by javaboyjunior99
Memory warnings are triggered when the OS is low on available memory - it might have nothing to do with your app. Something else happening on the device - an incoming text message, phone call, notification of some kind etc. all can cause memory warnings. With that said, you have to free up unused memory or you run the risk of your app being force quit by the OS.
There's some things you can do to help reduce your memory overhead. Only cache items you need cached, alloc and release objects as you use them rather than adding them to the autorelease pool. Auto released objects use memory until the pool is cleaned. For example creating an autoreleased NSString with
NSString* a = [NSString stringWithString:@"a"];
will get added to the autorelease pool and freed eventually. Instead, create it, use it and release it straight away.
NSString* a = [[NSString alloc] initWithString:@"a"];
// do something
This way the memory is only allocated for the time it is in use. If you're going to use auto release pools, create them and drain them close together. I keep an autorelease pool around even though I am allocing and releasing most of my objects and then I drain the pool on a memory warning which frees up some memory.
I hope this helps, I seem to remember that the SQLite books sample code had some examples on inflating objects on demand and deflating them on receipt of a memory warning. Good luck.