淺談IOS記憶體最佳化經驗是本文要介紹的內容,詳細的講解了IOS的記憶體最佳化方案,不多說,我們先來看詳細內容。
首先最最重要的還是確保每個retain,copy,delloc都帶有一個release
1.凡事有CT..Create..的要用CTRelease(myObject)釋放; CF等也同樣道理
2.下面的return前也沒有釋放
- CTParagraphStyleRef paragraphStyle=CTParagraphStyleCreate(paragraphStyle_settings,
- sizeof(paragraphStyle_settings) / sizeof(paragraphStyle_settings[0]));
-
- if(...)
- {
- <SPAN style="WHITE-SPACE: pre"> </SPAN>return; //這裡會溢出
- }
- CTRelease(paragraphStyle);
- CTParagraphStyleRef paragraphStyle=CTParagraphStyleCreate(paragraphStyle_settings,
- sizeof(paragraphStyle_settings) / sizeof(paragraphStyle_settings[0]));
- if(...)
- {
- return; //這裡會溢出
- }
- CTRelease(paragraphStyle);
3.有時NSMutableArray mutableCopy也會溢出
- NSMutableArray *mutableRecents = [NSMutableArray arrayWithArray:recentSearches];
- //NSMutableArray *mutableRecents = [recentSearches mutableCopy]; 這裡記憶體會溢出
- [mutableRecents removeObject:searchString];
- NSMutableArray *mutableRecents = [NSMutableArray arrayWithArray:recentSearches];
- //NSMutableArray *mutableRecents = [recentSearches mutableCopy]; 這裡記憶體會溢出
- [mutableRecents removeObject:searchString];
4.dealloc裡面的記憶體溢出大部分由init或initWithFrame不正當的初始化引起
5:先在Instrument下用模擬器檢查記憶體溢出,再用Instrument連真機檢查.
小結:淺談IOS記憶體最佳化經驗的內容介紹完了,希望本文對你有所協助!