在OC的API中使用,readonly 和 copy声明,一般都是一个计算值,而在循环体中直接使用计算属性则会大量消耗内存,此时最好的解决办法是在获取某个计算属性使用autoreleasepool来降低内存峰值, 另外autorelease的值在循环中也会大量消耗内存,当然也可以使用autoreleasepool来降低内存峰值;


下面两个测试例子
低内存:
while (true) {
NSString *a = [[NSString alloc] initWithFormat:@"test"];
}
高内存:
while (true) {
NSString *a = [NSString stringWithFormat:@"test"];
}
当然上面的高内存版,使用autoreleasepool可以降低内存
while (true) {
@autoreleasepool {
NSString *a = [NSString stringWithFormat:@"test"];
}
}
对于每一个Runloop, 系统会隐式创建一个Autorelease pool, 这样所有的release pool会构成一个象CallStack一样的一个栈式结构,在每一个Runloop结束时,当前栈顶的Autorelease pool会被销毁,这样这个pool里的每个Object会被release。
一个UI事件、Timer call、 delegate call、 都会是一个新的Runloop。
实际上对于 [NSString stringWithFormat:] 这类构造函数返回的对象都是autorelease的。
autorelease pool来避免频繁申请/释放内存(就是pool的作用了)!
总结:
1.一定要注意Autorelease pool的生存周期,理解Runloop,避免在对象被释放后使用。
2.[NSString stringWithFormat:]这类函数返回的对象是不需要再自己release的,它已经被autorelease了, MRC下如果你想把它当一个全局对象使用,那必须自己再retain, 释放时再release。


网友评论