我是新手iOS/Objective-C
,我不正确理解内存的释放。为了测试它,我创建了一个空的ARC
启用 iPhone-Project 并创建了一个非常简单的测试类:
#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
我只是在以下位置开始测试AppDelegate
:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
MemTest *test = [[MemTest alloc] init];
[test start];
....
}
应用程序(如预期的那样)打印了许多漂亮的数字“i = xy”,但内存使用量随着每次迭代而增加,最终应用程序崩溃:
....
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
所以我的问题是:为什么内存使用量会增长?
我认为通过分配 nil,应该释放内存,当使用ARC
. 我在这里想念什么?