IOS app killed for Low Memory but no Memory Warning received

前端 未结 2 508
暖寄归人
暖寄归人 2020-12-31 21:49

I\'m stuck with a problem for some time now and I would like to know if anyone can help. I\'m developing an IOS app (iPad) using a main navigation controller and a lot of UI

2条回答
  •  迷失自我
    2020-12-31 22:28

    A memory warning is sent as a notification, so it'll be queued up on the runloop for dispatch as soon as an opportunity arises. If you were to write a (deliberately broken) loop like:

    while(1)
    {
        NSString *newString = [NSString string];
    }
    

    Then eventually your app would be killed due to low memory but at no opportunity would it be in a position to receive a low memory warning.

    If you're being killed due to low memory without receiving a warning then you've probably creating a memory bottleneck for yourself somewhere, likely you have some sort of loop that leaves a lot of things in the autorelease pool — so, if you get all the way through the loop then the temporary objects vanish and hence have no long-term footprint, but they're accumulating all the time you remain in the loop.

    To avoid that sort of situation you want to look at nesting inner parts of the loop in their own NSAutoreleasePools. For example, this loop:

    while(1)
    {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        NSString *newString = [NSString string];
        [pool drain]; // stylistically preferred to release, but equivalent
                      // in reference counted environments
    }
    

    Will continue forever but won't ever trigger a low memory condition.

提交回复
热议问题