“Low Memory Warning” “Low Memory Crash” are the one’s that frightens iPhone Developers.
There are many posts in various blogs explaining how to efficiently utilize the available memory, and some have tips in memory management. Most of the Cases, as I have seen, these memory issues arises often due to some negligence/improper handling of resources, esp. images, views, cached data and so on.
As the saying goes “Prevention is better than cure”, This post FOCUS on
in a ViewController, which we use it very often, and to some extent is the root cause of Memory issues we encounter.
Here is a typical template of UIViewController:
// release all views this controller own
// do the initialization of class variables here..
should act as constructor for this class
STEP 1: A method, name it as “releaseAllViews“ , which is responsible to release all & ONLY the Views, referenced either by IBOutlets, or view elements created in viewDidLoad method or elsewhere in the class.
//call super method
self =[super initWithNibName:nibName bundle:nibBundle];
//Invoked when the class is instantiated in XIB
self = [super initWithCoder:aDecoder];
STEP 2: I would have a method which initializes all my class variables, also acts as a constructor, and this is typically invoked by “initWithNibName” (creating view controller programmatically) and “initWithCoder” (in case view controller is created in XIB).
//Release all your caches like say Image Cache, any File Data etc.
STEP 3: didReceiveMemoryWarning is invoked at various times at different Memory Levels of the application. Make sure you release all the heavy objects like Images, Data and so on and don’t forget to make sure that these cached data should be able to restore their state in future, without having any impact on the application.
//release all your views, which the controller owns, here
STEP 4: viewDidUnload is invoked when the system identifies that your view is no longer visible on screen, when the memory warning notification arrives. Make sure you call releaseAllViews to relinquish the ownership of the views the controller owns.
//remove as Observer from NotificationCenter, if this class has registered for any notifications
[[NSNotificationCentre defaultCenter] removeObserver:self];
//release all you member variables and appropriate caches
STEP 5: Release all your views, data cache, member variables, and remove as observer, incase the controller is registered to any of the notifications.
In Part-2 of this Post, I will provide Xcode Templates for UIViewController class which includes all the above steps.
Here I Leave this links which provides you tips in Memory Management in iPhone OS
One way to test the Memory Warning scenarios, one would use the Hardware -> Simulate Memory Warning menu in iOS Simulator. But, For some lazy people like me or for those smart people who wants the application to be periodically check for the memory warning at the background, I found a Code snippet from iDevRecipes
All you need to do is to place the below code in your App Delegate
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), (CFStringRef)@”UISimulatedMemoryWarningNotification”, NULL, NULL, true);
Place the below code in your application:didFinishLaunchingWithOptions: method (preferably in the first line of this method)
//lets call the simulate warning every 5 minutes (300 seconds)
[NSTimer scheduledTimerWithTimeInterval:300.0 target:self selector:@selector(simulateMemoryWarning) userInfo:nil repeats:YES];
NOTE: The above code snippets has to be placed between TARGET_IPHONE_SIMULATIOR and #endif to make sure that this code is only executed in the Simulator, as we are using thing which are undocumented and if used in App Store builds, it would violate Apple SDK policy and application could be rejected.