標籤:ios ios開發 記憶體管理 記憶體
iOS的UIViewController 類給我們提供了處理記憶體不足的介面。
在iOS 3.0 之前,當系統的記憶體不足時,UIViewController的didReceiveMemoryWarining 方法會被調用,我們可以在didReceiveMemoryWarining 方法裡釋放掉部分暫時不用的資源。
從iOS3.0 開始,UIViewController增加了viewDidUnload方法。
該方法和viewDIdLoad相配對。
當系統記憶體不足時,首先UIViewController的didReceiveMemoryWarining 方法會被調用,而didReceiveMemoryWarining 會判斷當前ViewController的view是否顯示在window上,
如果沒有顯示在window上,則didReceiveMemoryWarining 會自動將viewcontroller 的view以及其所有子view全部銷毀,然後調用viewcontroller的viewdidunload方法。
如果當前UIViewController的view顯示在window上,則不銷毀該viewcontroller的view,當然,viewDidunload也不會被調用了。
iOS3-iOS5.0以前版本收到記憶體警告:
調用didReceiveMemoryWarning內調用super的didReceiveMemoryWarning會將controller的view進行釋放。所以我們不能將controller的view再次釋放。
處理方法:
Java代碼
- -(void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];//如沒有顯示在window上,會自動將self.view釋放。
- // ios6.0以前,不用在此做處理,self.view釋放之後,會調用下面的viewDidUnload函數,在viewDidUnload函數中做處理就可以了。
- }
- -(void)viewDidUnload
- {
- // Release any retained subviews of the main view.不包含self.view
- //處理一些記憶體和資源問題。
- [super viewDidUnload];
-
- }
但是到了ios6.0之後,這裡又有所變化,ios6.0記憶體警告的viewDidUnload 被屏蔽,即又回到了ios3.0的時期的記憶體管理方式。
iOS6.0及以上版本的記憶體警告:
調用didReceiveMemoryWarning內調用super的didReceiveMemoryWarning調只是釋放controller的resouse,不會釋放view
處理方法:
-(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];//即使沒有顯示在window上,也不會自動的將self.view釋放。
// Add code to clean up any of your own resources that are no longer necessary.
// 此處做相容處理需要加上ios6.0的宏開關,保證是在6.0下使用的,6.0以前屏蔽以下代碼,否則會在下面使用self.view時自動載入viewDidUnLoad
if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0) {
//需要注意的是self.isViewLoaded是必不可少的,其他方式訪問視圖會導致它載入 ,在WWDC視頻也忽視這一點。
if (self.isViewLoaded && !self.view.window)// 是否是正在使用的視圖
{
// Add code to preserve data stored in the views that might be
// needed later.
// Add code to clean up other strong references to the view in
// the view hierarchy.
self.view = nil;// 目的是再次進入時能夠重新載入調用viewDidLoad函數。
}
}
}
根據網上的的翻譯理解是這樣的.一般不是官方的說法本人都會保留懷疑的態度.然後找到了官方的連結.
https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/ViewLoadingandUnloading/ViewLoadingandUnloading.html
IOS didReceiveMemoryWarning 的那些事