標籤:blog http java 使用 os strong
iPhone下每個app可用的記憶體是被限制的,如果一個app使用的記憶體超過20M,則系統會向該app發送Memory Warning訊息。收到此訊息後,app必須正確處理,否則可能出錯或者出現記憶體泄露。
app收到Memory Warning後會調用:UIApplication::didReceiveMemoryWarning -> UIApplicationDelegate::applicationDidReceiveMemoryWarning,然後調用當前所有的viewController進行處理。因此處理的主要工作是在viewController。
當我們的程式在第一次收到記憶體不足警告時,應該釋放一些不用的資源,以節省部分記憶體。否則,當記憶體不足情形依然存在,iOS再次向我們程式發出記憶體不足的警告時,我們的程式將會被iOS kill掉。
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也不會被調用了。但是到了ios6.0之後,這裡又有所變化,ios6.0記憶體警告的viewDidUnload 被屏蔽,即又回到了ios3.0的時期的記憶體管理方式。
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
- //處理一些記憶體和資源問題。
- 10. [super viewDidUnload];
- 11.
- 12. }
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函數。
}
}
}
但是似乎這麼寫相對於以前並不省事。最終我們找到一篇文章,文章中說其實並不值得回收這部分的記憶體,原因如下:
1. UIView是UIResponder的子類,而UIResponder有一個CALayer的成員變數,CALayer是具體用於將自己畫到螢幕上的。
2. CALayer是一個bitmap圖象的封裝類,當UIView調用自身的drawRect時,CALayer才會建立這個bitmap圖象類。
3. 具體占記憶體的其實是一個bitmap圖象類,CALayer只佔48bytes, UIView只佔96bytes。而一個iPad的全屏UIView的bitmap類會佔到12M的大小!
4.在iOS6時,當系統發出MemoryWarning時,系統會自動回收bitmap類。但是不回收UIView和CALayer類。這樣即回收了大部分記憶體,又能在需要bitmap類時,根據CALayer類重建。
所以,iOS6這麼做的意思是:我們根本沒有必要為了幾十byte而費力回收記憶體。
--------------------------切糕分割線--------------
PS:
1、關於這個的官方文檔:https://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ViewLoadingandUnloading/ViewLoadingandUnloading.html
2、zon2012貌似都沒有ios6的這個相容(其實view是沒問題的,關鍵是資源)
分享到:
xcode4.5(iOS 6)開發與之前的差異 | IOS發送Email的方法
- 2013-03-03 16:24
- 瀏覽 4798
- 評論(1)
- 分類:移動開發
- 相關推薦
評論
1 樓 嘯笑天 2013-05-14
行動裝置終端的記憶體極為有限,應用程式必須做好low-memory處理工作,才能避免程式因記憶體使用量過大而崩潰。
low-memory 處理思路
通常一個應用程式會包含多個view controllers,當從view跳轉到另一個view時,之前的view只是不可見狀態,並不會立即被清理掉,而是儲存在記憶體中,以便下一次的快速顯現。但是如果應用程式接收到系統發出的low-memory warning,我們就不得不把當前不可見狀態下的views清理掉,騰出更多的可使用記憶體;當前可見的view controller也要合理釋放掉一些快取資料,圖片資源和一些不是正在使用的資源,以避免應用程式崩潰。
思路是這樣,具體的實施根據系統版本不同而略有差異,本文將詳細說明一下iOS 5與iOS 6的low-memory處理。
iOS 5 的處理
在iOS 6 之前,如果應用程式接收到了low-memory警告,當前不可見的view controllers會接收到viewDidUnload訊息(也可以理解為自動調用viewDidUnload方法),所以我們需要在 viewDidUnload 方法中釋放掉所有 outlets ,以及可再次建立的資源。當前可見的view controller 通過didReceiveMemoryWarning 合理釋放資源,具體見代碼注釋。
舉一個簡單的例子,有這樣一個view controller:
@interface MyViewController : UIViewController {
NSArray *dataArray;
}
@property (nonatomic, strong) IBOutlet UITableView *tableView;
@end
對應的處理則為:
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn‘t have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc that aren‘t in use.
}
- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
self.tableView = nil;
dataArray = nil;
[super viewDidUnload];
}
iOS 6 的處理
iOS 6 廢棄了viewDidUnload方法,這就意味著一切需要我們自己在didReceiveMemoryWarning中操作。
具體應該怎麼做呢?
1.將 outlets 置為 weak
當view dealloc時,沒有人握著任何一個指向subviews的強引用,那麼subviews執行個體變數將會自動置空。
@property (nonatomic, weak) IBOutlet UITableView *tableView;
2.在didReceiveMemoryWarning中將快取資料置空
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
dataArray = nil;
}
不要忘記一點,每當tableview reload 的時候,需要判斷一下 dataArray ,若為空白則重新建立。
相容iOS 5 與 iOS 6
好吧,重點來了,倘若希望程式相容iOS 5 與 iOS 6怎麼辦呢? 這裡有一個小技巧,我們需要對didReceiveMemoryWarning 做一些手腳:
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
if ([self isViewLoaded] && self.view.window == nil) {
self.view = nil;
}
dataArray = nil;
}
判斷一下view是否是window的一部分,如果不是,那麼可以放心的將self.view 置為空白,以換取更多可用記憶體。
這樣會是什麼現象呢?假如,從view controller A 跳轉到 view controller B ,然後類比low-memory警告,此時,view controller A 將會執行self.view = nil ; 當我們從 B 退回 A 時, A 會重新調用一次 viewDidLoad ,此時資料全部重新建立,簡單相容無壓力~~
Note:
如果你好奇Apple為什麼廢棄viewDidUnload,可以看看Apple 的解釋:
Apple deprecated viewDidUnload for a good reason. The memory savings from setting a few outlets to nil just weren’t worth it and added a lot of complexity for little benefit. For iOS 6+ apps, you can simply forget about view unloading and only implement didReceiveMemoryWarning if the view controller can let go of cached data that you can recreate on demand later.