简体   繁体   中英

applicationDidReceiveMemoryWarning doesn't work when in background

I tried to simulate memory warning on iOS Simulator and it turned out that when the app was in the background, the function didn't get called at that moment, but after the app became active again that the applicationDidReceiveMemoryWarning was being called.

When tested with Instrument:Activity Monitor , my app didn't release any memory when in the background while the other apps memory consumption were getting less.

However, if I call applicationDidReceiveMemoryWarning via applicationDidEnterBackground , the memory get released correctly, but I don't want to release memory at this stage though.

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [glView applicationDidEnterBackground:application];

    // Function works if it's called from here, but not what I want.
    [self applicationDidReceiveMemoryWarning:application];
}

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
    [glView applicationDidReceiveMemoryWarning:application];
}

How do I fix this?

Unfortunately, what you want to do is discouraged by Apple in the iOS App Programming Guide . An app briefly enters the background, allowing you to free up memory. After that, the app is suspended and cannot receive messages, except applicationWillTerminate. If a low-memory warning fires, the operating system kills suspended apps as needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM