NSRunLoop中Autorelease pool 管理
来源:互联网 发布:网络精准推广 编辑:程序博客网 时间:2024/05/01 20:17
http://segmentfault.com/q/1010000000123664
iOS的运行时是由一个一个runloop组成的,每个runloop都会执行下图的一些步骤:
可以看到,每个runloop中都创建一个Autorelease Pool,并在runloop的末尾进行释放,
所以,一般情况下,每个接受autorelease消息的对象,都会在下个runloop开始前被释放。也就是说,在一段同步的代码中执行过程中,生成的对象接受autorelease消息后,一般是不会在代码段执行完成前释放的。
当然也有让autorelease提前生效的办法:自己创建Pool并进行释放
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];NSArray * array = [[[NSArray alloc] init] autorelease];[pool drain];
上面的array就会在[pool drain]执行时被释放。
所以对于你遇到的问题,可以在for循环外嵌套一个Autorelease Pool进行管理,例如
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];for (int i = 0; i < 10000; i++){ // ... }[pool drain];
但由于你提到了生成的每个实例可能会比较大。只在循环外嵌套,可能导致在pool释放前,内存里已经有10000个实例存在,造成瞬间占用内存过大的情况。
因此,如果你的每个实例仅需要在单次循环过程中用到,那么可以考虑可以在循环内创建pool并释放
for (int i = 0; i < 10000; i++){ NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; // ... [pool drain];}
如果还有疑问,可以参考下面几篇资料:
How does the NSAutoreleasePool Work
http://stackoverflow.com/questions/65...
NSAutoreleasePool Class Reference
https://developer.apple.com/library/m...
Autorelease is fast:
http://www.mikeash.com/pyblog/autorel...
- NSRunLoop中Autorelease pool 管理
- IOS基本内存管理:autorelease和autorelease pool
- autorelease pool
- Autorelease Pool
- release,autorelease,autorelease pool
- Objective C内存管理——如何理解autorelease pool
- autorelease pool的内存管理之-自动释放池
- 黑马程序员------OC 内存管理MRC和autorelease pool
- Using Autorelease Pool Blocks
- Objective-C----autorelease pool
- OC中Autorelease Pool实现原理与autorelease何时被释放
- 内存管理中autorelease()的调用
- cocoa内存autorelease pool原理
- cocos2d-x中的autorelease pool
- Apple官方autorelease pool译文
- ios学习之autorelease pool
- 什么时候应该使用Autorelease Pool
- 正确使用 autorelease pool blocks
- 项目中那些事|string.xml 那些不为人知的事
- sqlite循环批量插入数据
- 桃花坞-程序员版
- JDk安装及环境变量的配置
- 分享正在执行的事务的基本情况
- NSRunLoop中Autorelease pool 管理
- [VC6] RadioBox使用入门
- response.sendRedirect方式的转向与RequestDispatcher的forward方法的比较
- 基于jquery的循环左右滚动和上下滚动效果(备用记录)
- STM学习资料下载链接
- cocos2d坐标系
- 告别ide 之UEdit32的Java环境配置
- 关于oracle远程1521不通的问题
- oracle数据库连接信息的初始用户名和口令