我遇到了導致崩潰的內存問題。我正在循環訪問字典數組,然後在循環中通過我創建的鍵數組進行循環。我使用該鍵數組中的每個鍵來獲取字典中該鍵的值。然後我通過附加值創建一個字符串。該字符串將包含大量的數據。iPhone:在循環中創建NSString時產生巨大的內存峯值
我也在使用ARC,因此我無法手動發佈。
內存尖峯發生在stringByAppendingFormat行上。
NSString *theString = [[NSString alloc] init];
for (NSMutableDictionary *aDict in collectionArray)
{
for (NSString *key in itemKeys)
{
NSString *valueString = [aDict valueForKey:key];
// Memory spikes here
theString = [theString stringByAppendingFormat:@"%@,", valueString];
}
}
aDict字典和itemKeys數組中有多少個條目? – ThomasW 2012-03-15 01:03:18
你正在泄漏'theString'。一遍又一遍地。 – 2012-03-15 01:03:32
也許一個NSMutableString(https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableString_Class/Reference/Reference.html#//apple_ref/occ/instm/NSMutableString/)會對此更合適。你可以對它使用appendString。您也可以預先預先計算所需的緩衝容量 – Ben 2012-03-15 01:05:51