I am new to iOS/Objective-C and I do not understand the memory release correctly. To test it, I created an empty ARC iPhone project and created a very simple test class:
#import "MemTest.h" @implementation MemTest { } -(void) start { for (int i = 0; i < 1500000; i++) { NSMutableString *myString = [NSMutableString string]; // The appended string is 2000 characters long in the real test class. [myString appendString:@"12345678901234567890123456 <very long>"]; if (i % 1000 == 0) { NSLog(@"i = %d", i); } myString = nil; } } @end
I just run the test in AppDelegate :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { MemTest *test = [[MemTest alloc] init]; [test start]; .... }
The application (as expected) prints a lot of good "i = xy" numbers, but memory usage increases with each iteration, and finally the application crashes:
.... 2012-12-06 20:17:40.193 MemTestApp[19250:11303] i = 930000 2012-12-06 20:17:40.208 MemTestApp[19250:11303] i = 931000 MemTestApp(19250,0xac63f2c0) malloc: *** mmap(size=16777216) failed (error code=12) *** error: can't allocate region *** set a breakpoint in malloc_error_break to debug
So my question is: why is memory usage increasing at all?
I thought by assigning nil, the memory should be released when using ARC . What am I missing here?
Leif
source share