IOS didReceiveMemoryWarning 的那些事

來源:互聯網
上載者:User

標籤: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代碼  
  1. -(void)didReceiveMemoryWarning  
  2.        {  
  3.                 [super didReceiveMemoryWarning];//如沒有顯示在window上,會自動將self.view釋放。  
  4.                 // ios6.0以前,不用在此做處理,self.view釋放之後,會調用下面的viewDidUnload函數,在viewDidUnload函數中做處理就可以了。  
  5.        }  
  6.        -(void)viewDidUnload  
  7.        {  
  8.               // Release any retained subviews of the main view.不包含self.view  
  9.               //處理一些記憶體和資源問題。  
  10.                [super viewDidUnload];  
  11.                
  12.        }  
 


但是到了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 的那些事

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.