low-memory 處理思路

移動設備終端的內存極爲有限,應用程序必須做好low-memory處理工作,才能避免程序因內存使用過大而崩潰。

 

注意:iboutlet作爲私有變量時,可以設置形如

__weak IBOutlet UILabel *twoLB;的即可實現釋放效果,再次didload時,會重新鏈接相應控件。

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 ,若爲空則重新創建。


 注意:iboutlet作爲私有變量時,可以設置形如

__weak IBOutlet UILabel *twoLB;的即可實現釋放效果,再次didload時,會重新鏈接相應控件。


兼容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.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章