淺談IOS記憶體最佳化經驗

來源:互聯網
上載者:User

淺談IOS記憶體最佳化經驗是本文要介紹的內容,詳細的講解了IOS記憶體最佳化方案,不多說,我們先來看詳細內容。

首先最最重要的還是確保每個retain,copy,delloc都帶有一個release

1.凡事有CT..Create..的要用CTRelease(myObject)釋放; CF等也同樣道理 

2.下面的return前也沒有釋放

 
  1. CTParagraphStyleRef paragraphStyle=CTParagraphStyleCreate(paragraphStyle_settings, 
  2. sizeof(paragraphStyle_settings) / sizeof(paragraphStyle_settings[0]));     
  3.     
  4. if(...)     
  5. {     
  6. <SPAN style="WHITE-SPACE: pre"> </SPAN>return; //這裡會溢出     
  7. }        
  8. CTRelease(paragraphStyle);    
  9. CTParagraphStyleRef paragraphStyle=CTParagraphStyleCreate(paragraphStyle_settings, 
  10. sizeof(paragraphStyle_settings) / sizeof(paragraphStyle_settings[0]));  
  11. if(...)  
  12. {  
  13.  return; //這裡會溢出  
  14. }  
  15. CTRelease(paragraphStyle);   

3.有時NSMutableArray mutableCopy也會溢出

 
  1. NSMutableArray *mutableRecents = [NSMutableArray arrayWithArray:recentSearches];     
  2. //NSMutableArray *mutableRecents = [recentSearches mutableCopy]; 這裡記憶體會溢出     
  3. [mutableRecents removeObject:searchString];    
  4. NSMutableArray *mutableRecents = [NSMutableArray arrayWithArray:recentSearches];  
  5. //NSMutableArray *mutableRecents = [recentSearches mutableCopy]; 這裡記憶體會溢出  
  6. [mutableRecents removeObject:searchString];    

4.dealloc裡面的記憶體溢出大部分由init或initWithFrame不正當的初始化引起

5:先在Instrument下用模擬器檢查記憶體溢出,再用Instrument連真機檢查.

小結:淺談IOS記憶體最佳化經驗的內容介紹完了,希望本文對你有所協助!

聯繫我們

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