Reputation: 23550
When the app receives a low memory warning message, 3 situations can happen :
So when you receive this message, your are supposed to free memory... But where ? And how ?
I understand that :
initWith
..... must set the default static values. viewDidLoad
must load any non static objectdidReceiveMemoryWarning
must free those non static objectsviewDidUnload
...I guess some retained values must be set to nil somewhere... in didReceiveMemoryWarning ?
And what must be done with the active context (positions of thing of the screen, displayed text, ...) so when viewDidLoad is called again, those thing appear again as they where before the memoryWarning call ?
I mean, imagine 2 scenarios :
Scenario 1
Scenario 2
So when those memory warning happens, do you have any other choice than writing things to disk to display them back later ?
And when do you load those again ? I have a viewController that loads (viewDidLoad), receive a memoryWarning, unloads (viewDidUnload), but when going back to it, viewDidLoad is not called again ? Do this must be done in viewWillAppear ? Do I have to think that anytime viewWillAppear is triggered, I can assume that things that are supposed to be displayed on it are loaded ?
Any help, even with valuable links, would be great !
Thank you for your help.
Upvotes: 2
Views: 3350
Reputation: 70733
Consider the alternatives to your scenarios. Your app could be killed if it does not free enough memory, which would be even more jarring to the user. One might choose potentially flickering the current display over losing the user's valuable data.
Upvotes: 1
Reputation: 2292
My idea is that two methods are called when an app receives a low memory warning:
didReceiveMemoryWarning // in your NSObjects
and
applicationDidReceiveMemoryWarning // in your app delegate
then, if you want to free memory, these are the methods to consider.
As regards what you can do in there... Well... Think about what Xcode suggests:
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
/*
Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
*/
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
Seemingly, the best choise will be freeing any object not seen by the user and/or not in use, or any cached data that you can recreate later. Do not touch your GUI: if you close it or part of it your app becomes unusable and the user disappointed.
Regarding your 2 scenarios, I see a possible mistake in considering what memory warning are for. They are a way to treat an emergency and not the normal way to manage memory. Developpers must think of a good memory architecture and save data whenever possible.
In scenario 1, save your data when the app is sent to background.
applicationDidEnterBackground
In scenario 2, save your data when a new view is opened.
Hope this makes sense...
Upvotes: 1