善用 NSAutoreleasePool 节约宝贵内存
2013-03-20 00:15
274 查看
autorelease自动释放内存,并不会立即把内存释放掉,而是要等到下一个事件周期才会释放掉。问题是一些资源我们不得不使用autorelease类型,比如作为函数的返回值,而且系统api及项目是的大部分也都是这么做的,如果全都依靠我们手动释放很容易造成内存泄漏。
01 | for (int i = 0; i <= 10000; i ++) { |
02 |
03 | //创建一个自动释放池 |
04 |
05 | NSAutoreleasePool *pool = [NSAutoreleasePool new]; |
06 |
07 | NSString *filePath = [[NSBundle mainBundle] pathForResource:@"hf" ofType:@"PNG"]; |
08 |
09 | UIImage *image = [[UIImage alloc] initWithContentsOfFile:filePath]; |
10 |
11 | UIImage *scalimage = [image imageByScalingAndCroppingForSize:CGSizeMake(320, 640)]; |
12 |
13 | [image release]; |
14 |
15 | //将自动释放池内存释放,它会同时释放掉上面代码中产生的临时变量image2 |
16 |
17 | [pool drain]; |
18 |
19 | } |
相关文章推荐
- 特别备注:NSData可能会引起内存增长,尤其是在线程,就算用了NSAutoReleasePool也不管用
- iOS---NSAutoreleasePool自动释放原理及详解
- NSAutoReleasePool使用中drain和release的区别
- ISO基本内存管理:autorelease与autoreleasepool
- cocoa内存autorelease pool原理
- NSAutoreleasePool初始化语句必须是第一条执行语句
- Autoreleasepool自动释放池与内存相关
- NSAutoReleaseNoPool(): Object 0x********* of class NSConreteData autoreleased with no pool in place
- Xcode中使用NSAutoreleasePool报错的问题
- Foundation框架经常使用数据类型和NSAutoreleasePool自己主动释放池解析
- iPhone中objective-c NSAutoreleasePool 的一些理解
- Objective-C NSAutoreleasePool
- NSAutoReleasePool使用中drain和release的区别
- Xcode中使用NSAutoreleasePool报错的问题
- iOS---NSAutoreleasePool自动释放原理及详解
- NSAutoReleasePool 用法
- NSAutoReleasePool使用中drain和release的区别
- NSAutoReleasePool
- NSAutoreleasePool实现原理
- iPhone中objective-c NSAutoreleasePool 的一些理解