使用 NSJSONSerialization 在大循环中使用 ARC 的内存问题
Memory issue using ARC in big for loop using NSJSONSerialization
我目前正在尝试创建一个函数,从 NSString 进行大量 json 序列化。
NSArray* array = nil;
NSError* error = nil;
for (NSObject* obj in otherArray) {
array = [NSJSONSerialization JSONObjectWithData:[obj.json dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
// I'm using array here .. and then i don't need it anymore
}
这里我的 otherArray 可能非常大 obj.json。
但一段时间后,应用程序因内存问题 (> 1GB) 而崩溃。
似乎我的数组在 for 循环中永远不会 dealloc 因为当我评论该行时我没有收到任何错误..
我怎样才能使用 ARC 释放内存?
谢谢
在循环中使用自动释放池块来减少程序的内存占用:
NSArray* array = nil;
NSError* error = nil;
for (NSObject* obj in otherArray) {
@autoreleasepool {
array = [NSJSONSerialization JSONObjectWithData:[obj.json dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
// I'm using array here .. and then i don't need it anymore
}
}
Many programs create temporary objects that are autoreleased. These
objects add to the program’s memory footprint until the end of the
block. In many situations, allowing temporary objects to accumulate
until the end of the current event-loop iteration does not result in
excessive overhead; in some situations, however, you may create a
large number of temporary objects that add substantially to memory
footprint and that you want to dispose of more quickly. In these
latter cases, you can create your own autorelease pool block. At the
end of the block, the temporary objects are released, which typically
results in their deallocation thereby reducing the program’s memory
footprint.
我目前正在尝试创建一个函数,从 NSString 进行大量 json 序列化。
NSArray* array = nil;
NSError* error = nil;
for (NSObject* obj in otherArray) {
array = [NSJSONSerialization JSONObjectWithData:[obj.json dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
// I'm using array here .. and then i don't need it anymore
}
这里我的 otherArray 可能非常大 obj.json。
但一段时间后,应用程序因内存问题 (> 1GB) 而崩溃。 似乎我的数组在 for 循环中永远不会 dealloc 因为当我评论该行时我没有收到任何错误.. 我怎样才能使用 ARC 释放内存?
谢谢
在循环中使用自动释放池块来减少程序的内存占用:
NSArray* array = nil;
NSError* error = nil;
for (NSObject* obj in otherArray) {
@autoreleasepool {
array = [NSJSONSerialization JSONObjectWithData:[obj.json dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];
// I'm using array here .. and then i don't need it anymore
}
}
Many programs create temporary objects that are autoreleased. These objects add to the program’s memory footprint until the end of the block. In many situations, allowing temporary objects to accumulate until the end of the current event-loop iteration does not result in excessive overhead; in some situations, however, you may create a large number of temporary objects that add substantially to memory footprint and that you want to dispose of more quickly. In these latter cases, you can create your own autorelease pool block. At the end of the block, the temporary objects are released, which typically results in their deallocation thereby reducing the program’s memory footprint.