Reputation: 93
I am writing on a iOS program and I have a memory leak. I reproduce it on macOS, which I don't know how to solve it. It is very simple.
int main(int argc, const char * argv[]) {
@autoreleasepool {
system("top -l 1 | grep LEAK"); // baseline
NSMutableData* dataobj = [NSMutableData dataWithLength:DATA_SIZE];
system("top -l 1 | grep LEAK"); // baseline + 8 bytes
memset([dataobj mutableBytes], 1, DATA_SIZE);
system("top -l 1 | grep LEAK"); // baseline + DATA_SIZE
dataobj = nil;
system("top -l 1 | grep LEAK"); // baseline + DATA_SIZE
}
return 0;
}
In the end, I am expecting memory usage at baseline again, but it is not. Waiting for 10 seconds still the same result. I believe it must be something wrong with my understanding of ARC or NSData
.
Thank everyone who takes a look at it in advance.
Upvotes: 2
Views: 327
Reputation: 17721
You're creating your data with:
NSMutableData* dataobj = [NSMutableData dataWithLength:DATA_SIZE];
Due to the Memory Management Policy you're not an owner of this object, and it is placed in the Autorelease Pool. Since you're code is completely embedded in an @autoreleasepool { ... }
, it can't detect a deallocation of the data, because it is kept by this pool.
You're code doesn't contain a leak, and the operating system is responsible for the cleaning. This generally makes the success of your experiment very questionable. Finally, I would like to endorse Amin's comment: You're using the wrong methods. You should use Instruments instead.
Upvotes: 1