I started to monitor app memory usage using technique described in here: Programmatically retrieve memory usage on iPhone
I wrote 3 tests to try it out and this is what I found:
- (void)test1 {
for (int i = 0; i < 1000; i++) {
NSMutableString *str = [NSMutableString stringWithString:@""];
for (int j = 0; j < 1000; j++) {
[str appendString:@"some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string "];
}
}
- (void)test2 {
for (int i = 0; i < 100000; i++) {
@autoreleasepool {
NSString *stri = @"";
stri = [NSString stringWithFormat:@"%d some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really", i];
}
}
- (void)test3 {
NSString *str = @"";
for (int i = 0; i < 500; i++) {
str = [str stringByAppendingFormat:@"%d some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really long string some really", i];
}
When I call test1 or test3 memory gets allocated and deallocated properly - I can see it using the report_memory function described in the link above. But when I call test2 memory does not get deallocated - report_memory goes up and up. If I call test2 several times my app receives memory warning and gets terminated.
I'm using ARC. Can anyone explain what's going in here?