1 Reply Latest reply: Oct 23, 2008 8:55 PM by javaboyjunior99
tgersic Level 1 Level 1 (0 points)
What exactly triggers a memory warning? I have an application that is routinely getting memory warnings, but I'm only showing around 2MB of "Net Bytes" used in ObjectAlloc. This is after taking of photos with the camera, doing some image manipulations, and saving them to a database, but the memory is freed each time. I don't understand why I get memory warnings when I'm using hardly any memory...

MacBook Pro, Mac OS X (10.5.5)
  • javaboyjunior99 Level 1 Level 1 (50 points)
    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
    [a release];

    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.

    Mike