iPhone memory warning guidelines
Are there any specific guidelines of what to do if you get a memory warning? I know that you have to try and kill off objects that aren't required, but if this makes your application inoperable, what other options are there?
An example might be if say the application was Apple's contacts app and the user is editing a person's contact details. What could you do? Stop the edit? close down an开发者_开发百科d jump to the list screen?
Any advice or guidelines would be most appreciated.
In regard to view controllers' view management and memory warnings:
UIKit doesn’t only allow navigation back from a view controller, but also allows navigation to other view controllers from existing ones. In such a case, a new UIViewController will be allocated, and then loaded into view. The old view controller will go off-screen and becomes inactive, but still owns many objects – some in custom properties and variables and others in the view property/hierarchy. And so does the new visible view controller, in regard to its view objects.
Due to the limited amount of memory of mobile devices, owning the two sets of objects – one in the off-screen view controller and another in the on-screen view controller – might be too much to handle. If UIKit deems it necessary, it can reclaim some of the off-screen view controller’s memory, which is not shown anyway; UIKit knows which view controller is on-screen and which is off-screen, as after all, it is the one managing them (when you call presentModalViewController:animated: or dismissModalViewControllerAnimated:). So, every time it feels pressured, UIKit generates a memory warning, which unloads and releases your off-screen view from the view hierarchy, then call your custom viewDidUnload method for you to do the same for your properties and variables. UIKit releases self.view automatically, allowing us then to manually release our variables and properties in our viewDidUnload code. It does so for all off-screen view controllers.
When the system is running out of memory, it fires a didReceiveMemoryWarning. Off-screen views will be reclaimed and released upon memory warning, but your on-screen view will not get released – it is visible and needed. In case your class owns a lot of memory, such as caches, images, or the like, didReceiveMemoryWarning is where you should purge them, even if they are on-screen; otherwise, your app might be terminated for glutting system resources. You need to override this method to make sure you clean up your memory; just remember you call [super didReceiveMemoryWarning];.
An even more elaborate explanation is available here: http://myok12.wordpress.com/2010/11/30/custom-uiviewcontrollers-their-views-and-their-memory-management/
If your app is in the foreground and needs all the memory it has currently allocated to present a good user experience, then a perfectly reasonable option is to do nothing after getting a memory warning, and hope the OS kills or memory starves some other process.
But it might be a good idea to at least save important state in case your app isn't so lucky.
they will not specify how much you need to free... you just destroy things you're able to rebuild (e.g. those which are offscreen). don't steal/destroy what the user is looking at. ultimately, the system may just terminate your app if it needs the resources. i just take the approach: if offscreen, purge medium+large allocs which can be rebuilt when the user navigates back to that screen, and try to keep your memory consumption low to begin with - measure how much you use in test runs.
精彩评论