我目前正在解析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或沿着这些线的东西
只调用[self]。
-
您是否同时从两个线程访问相同的可变数组?这是不可靠的。
-
正如其他人所说,不要从后台线程更新表。
-
JSON文件有多大?您实际上是以一种效率较低的方式将所有数据存储在RAM中,因此预计原始文本的内存使用量将是其两倍。
-
试试Apple的JSON解析器。
内存使用率高是因为我将JSON项存储在NSArray中,它保留在内存中。我能够通过使用realm将对象缓存到磁盘并在需要时调用它们来解决这个问题,就像eric建议的那样。