What to do when my app receives memory warning?

后端 未结 4 402
说谎
说谎 2021-01-12 13:21

What should I do when my app recieves a memory warning?

相关标签:
4条回答
  • 2021-01-12 13:55

    It all depends on your app, usually you don't have to do anything special except following Apple's recommended practices.

    ViewControllers which are not visible at the moment will get didReceiveMemoryWarning message. By default (calling [super didReceiveMemoryWarning]) controller's view is unloaded (released, freed). As the view is unloading, view controller receives viewDidUnload where you should release all your IBOutlets (or otherwise retained UI elements). Only then the view can completely be deallocated and memory freed.

    In the didReceiveMemoryWarning you should also free as much data as you can - if you store some part of data model in ViewController, release it, and reconstruct in viewDidLoad that would be called when your view is loaded again (when user navigates back to this controller). You can inform your model classes to free memory too.

    0 讨论(0)
  • 2021-01-12 14:06

    It really depends on your app.

    If your app downloads and caches lots of contents from Internet for example, you should purge as much as possible when receiving a warning.

    If your app is an OpenGL game, you might have a texture/sound/data manager which references some unused data, which you then want to free. Cocos2D manages this kind of things.

    If your app isn't memory intensive, you have a memory leak somewhere, and you should 1) read the Memory Management Programming Guide by Apple 2) use Instruments/Leaks.

    0 讨论(0)
  • 2021-01-12 14:06

    In didReceiveMemoryWarning, you should release any cached or non-essential items to prevent running out of memory completely.

    0 讨论(0)
  • 2021-01-12 14:15

    If you log or write to any other file, there might be an issue with "disk" space.

    Also you should check for memory leaks.

    0 讨论(0)
提交回复
热议问题