1.使用@autoreleasepool在生成大量对象时使用
for (int i = 0; i < 500000; i++) {
@autoreleasepool {
// UIView* view = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
NSNumber *num = [NSNumber numberWithInt:i];
NSString *str = [NSString stringWithFormat:@"%d ", i];
//Use num and str...whatever...
[NSString stringWithFormat:@"%@%@", num, str];
printf("-----%lf\n", getMemoryUsage());
}
}
记住:千万不要把@autoreleasepool写在for循环外面,否则无效!!!!
2.到底什么时候释放?
这个问题一直困扰着我,现在终于明白了。是在一次runloop之后释放,不是runlooop stop的时候,而是每次休眠等待下次事件的时候会释放