【发布时间】:2015-07-28 15:23:00
【问题描述】:
我目前正在从 JSON 解析我的所有数据并将其存储在一个数组中。然而,当它开始解析时,内存使用量从大约 25mb 跃升至 800mb。在做了一些研究之后,我被告知在 GCD 块中放置一个 @autoreleasepool 但无济于事。
这是我目前得到的代码:
self.channelSchedules = [NSMutableArray new];
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Add code here to do background processing
//Loop through each channel object and download schedule
@autoreleasepool {
for (atlas_channel* channel in self.channels) {
NSLog(@"Updating listings for %@", [channel getChannelTitle]);
[self.channelSchedules addObject:[[channel getChannelSchedule] returnCurrentContentObject]];
[self.tableView reloadData];
[self scrollViewDidScroll:nil];
}
}
dispatch_async( dispatch_get_main_queue(), ^{
// Add code here to update the UI/send notifications based on the
// results of the background processing
[self.tableView reloadData];
});
});
我正在使用 TouchJSON 来解析数据。
经过进一步的研究,我认为这与我将所有值一旦解析成一个 NSArray 就将其存储在内存中的事实有关。我想我将不得不使用 CoreData 或类似的东西。
【问题讨论】:
-
如果您的 autoreleasepool 在 for 循环内,您将在每次迭代中得到耗尽。可能有更好的方法来设计此代码以避免内存,但这可能是一个快速修复。
-
它已减半至约 400mb。不过,这似乎仍然很高
-
你要解析多少数据? @autoreleasepool 在这里没用(你的整个应用不都在@autoreleasepool 下吗?)
-
我正在解析一个 JSON 文件,然后对于返回的每个条目,我必须解析另外两个 JSON 文件。这是因为 atlas 的工作方式。我正在使用 metabroadcast 中的 Atlas API。
标签: objective-c json memory-management memory-leaks touchjson