Will dealloc
be called when under memory pressure? If it's true, is it called before didReceiveMemoryWarning
or after? Also, when the view controller is being released, is it for sure that dealloc
will be called?
The didReceiveMemoryWarning
is simply informing you that you are low in memory. Neither the view nor the controller is released. On the other hand, prior to iOS 6, you might received viewDidUnload
at the same time, which is informing you that the view has been removed (but not the view controller). That's why you override didReceiveMemoryWarning
, so you can also free up any other stuff that your view controller can safely release. But these low memory situations, the view controller is never released. Worst case scenario, if viewDidUnload
was called, the view will be released, but not the controller.
FYI, I quote from the documentation:
Memory is a critical resource in iOS, and view controllers provide built-in support for reducing their memory footprint at critical times. The
UIViewController
class provides some automatic handling of low-memory conditions through itsdidReceiveMemoryWarning
method, which releases unneeded memory.When a low-memory warning occurs, the
UIViewController
class purges its views if it knows it can reload or recreate them again later. If this happens, [if running iOS versions prior to 6.0] it also calls theviewWillUnload
andviewDidUnload
methods to give your code a chance to relinquish ownership of any objects that are associated with your view hierarchy, including objects loaded from the nib file, objects created in yourviewDidLoad
method, and objects created lazily at runtime and added to the view hierarchy.
and
[
didReceiveMemoryWarning
] is called when the system determines that the amount of available memory is low. The default implementation of this method attempts to release the view controller’s view [in iOS versions prior to 6.0]. To be released, the view must not have a superview; that is, it is not part of a view hierarchy. For more information on how views are unloaded, see “The View Controller Life Cycle”.You can override this method to release any additional memory used by your view controller. If you do, your implementation of this method must call the super implementation at some point to allow the view controller to release its view. If your view controller holds references to views in the view hierarchy, you should release those references in the
viewDidUnload
method instead.
Update:
In answer to your other question (which you may not care about now that you realize that view controllers are not, themselves, released when the didReceiveMemoryWarning
is invoked), yes, when the item is finally released (and has a retain count of zero, i.e. the alloc and all of the various retains are now offset by corresponding release statements), then dealloc
will be invoked.