I'am currently trying to create a function that make a lot json serialization from NSString.
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
}
Here my otherArray could be quite large and obj.json too.
But after a while the application is crashing because of memory issue (> 1GB). It seems that my array is never dealloc in the for loop because when I comment the line I don't get any error .. How can I do to free the memory using ARC ?
Thanks
Use an autorelease pool block inside the loop to reduce the program’s memory footprint:
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.