ios 記憶體不足的處理(ios6 與 ios 6之前分部處理)

來源:互聯網
上載者:User

   

 自從iPhone4 支援多任務後,我們需要更加仔細處理記憶體不足的情形。如果使用者運行我們程式的時候,後台還跑著N個軟體,那前台啟動並執行iphone 程式就很容易收到記憶體不足的警告。

        通常情況下,iOS在記憶體不足時會給使用者一次處理記憶體資源的機會。當我們的程式在第一次收到記憶體不足警告時,應該釋放一些不用的資源,以節省部分記憶體。否則,當記憶體不足情形依然存在,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-iOS6.0以前版本收到記憶體警告:
調用didReceiveMemoryWarning內調用super的didReceiveMemoryWarning會將controller的view進行釋放。所以我們不能將controller的view再次釋放。
處理方法:
        -(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及以上版本的記憶體警告:
調用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時自動載入viewDidLoad

           

             float sysVer =[[[UIDevicecurrentDevice] systemVersion] floatValue];

            if (sysVer>= 6.0f)

            {

                 if ([self.view window] == nil)// 是否是正在使用的視圖
             {
                   // 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函數。
             }

            }

    }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.