I try to write a program in Objective C that calculates some stuff. Unfortunately there is a lot to calculate and the Program is running a loop with 10000 iterations. The total running time is around 5 minutes.
This isn’t an issue but while it is running the memory the program uses goes up dramatically (above to 2GB) and I figure it is because of some Values that are not released. I narrowed the problem down to a creation of a NSArray inside a loop. The problematic code (changed it to concentrate on the problem) is this:
for (NSInteger k = 0; k<100000; k++) {
NSMutableArray *mutableTestArray = [NSMutableArray new];
for (NSInteger i=0; i<200; i++){
NSMutableArray *subArray = [NSMutableArray new];
for (NSInteger j=0; j<200; j++) {
//simplified version, normally the valus for sub array are dependent on some other factors
[subArray addObject:[NSArray arrayWithObjects:@"test", @"test", nil]];
}
[mutableTestArray addObject:subArray];
}
//do stuff with mutableTestArray
//change parameters that go into sub array
}
If I replace:
[subArray addObject:[NSArray arrayWithObjects:@"test", @"test", nil]];
with:
[subArray addObject:@"test"];
it works fine. So I guess I somehow have to release the Array I created. But how?
I have ARC enabled and as far as I know if it is enabled I can’t release objects manually. It would be great if someone could help me with how to change the code or hint me somewhere where I can learn how to do memory management correctly.
Thanks
Just throw in some autoreleasepools :