Objective-C Memory Management
Objective-C Memory Management
Using Reference Counting
每一个从NSObject派生的对象都继承了对应的内存管理的行为。这些类的内部存在一个称为retain count的计数器。使用对应的调用,这个计数器会做自增或者自减。Objective-C runtime知道这个计数器何时变为0,此时会做deallocated。当对应deallocated时,它所占用的所有memory都会被释放。
有三种方法可以让retain count增加:
- 当你使用含有"alloc","create"的api来创建对象时,这个对象的retain count为1;
- 另外,当你通过一个包含"copy"的方法来得到一个对象时,这个对象的retain count为1;
你可以通过手动调用"retain"方法来增加retain count;
当你要减小retain count时,你需要调用release。
A standard allocation of an objectBar foo = [ [ Bar alloc ] init ];
Retaining an object*Bar* foo = [ [ Bar alloc ] init ];
[ foo retain ];
Using autorelease
Returning an autoreleased object
-( Foo* )getFoo {
Foo* foo = [ [ Foo alloc ] init ];
//do something with foo here...
return [ [ foo autorelease ] ];
}
The alloc/autorelease pattern
-( void )someMethod {
Foo* foo = [ [ Foo alloc ] init ] autorelease ];
//foo is still vaild here,
//it won't be released until the method exists
[ foo doSomething ];
}
Using factory methods versus the traditional allocation pattern
-( void )usingFactories {
NSMutableArray* array = [ NSMutableArray array ]; //nice, simple, autoreleased.
NsMutableArray* array2 = [ [ NSMutableArray alloc ] init ];
// do stuff with array and array2...
// need to release this one.
[ array2 release ];
// [ array release ]; no need to release this,
// it's already autoreleased
// if you release it here, it will cause a crash
}
Creating your own autorelease pool
-( void )inflateMemoryUsage {
for( NSUInteger n = 0; n < 100000; ++n ) {
NSAutoreleasePool *pool = [ [ NSAutoreleasePool alloc ] init ];
//this object is autoreleased
NSdata* data = [ self getBigBlobofData ];
// do something with data...
[ self doStuff:data ];
[ pool release ]; // the autoreleased objects are deallocated here.
}
//nothing left over
}
Objective-C Memory Management的更多相关文章
- Objective -C Memory Management 内存管理 第一部分
Objective -C Memory Management 内存管理 第一部分 Memory management is part of a more general problem in pr ...
- Memory Management in Open Cascade
Open Cascade中的内存管理 Memory Management in Open Cascade eryar@163.com 一.C++中的内存管理 Memory Management in ...
- Java (JVM) Memory Model – Memory Management in Java
原文地址:http://www.journaldev.com/2856/java-jvm-memory-model-memory-management-in-java Understanding JV ...
- Operating System Memory Management、Page Fault Exception、Cache Replacement Strategy Learning、LRU Algorithm
目录 . 引言 . 页表 . 结构化内存管理 . 物理内存的管理 . SLAB分配器 . 处理器高速缓存和TLB控制 . 内存管理的概念 . 内存覆盖与内存交换 . 内存连续分配管理方式 . 内存非连 ...
- Android内存管理(2)HUNTING YOUR LEAKS: MEMORY MANAGEMENT IN ANDROID PART 2
from: http://www.raizlabs.com/dev/2014/04/hunting-your-leaks-memory-management-in-android-part-2-of- ...
- Android内存管理(1)WRANGLING DALVIK: MEMORY MANAGEMENT IN ANDROID PART 1
from : http://www.raizlabs.com/dev/2014/03/wrangling-dalvik-memory-management-in-android-part-1-of-2 ...
- Understanding Memory Management(2)
Understanding Memory Management Memory management is the process of allocating new objects and remov ...
- Java Memory Management(1)
Java Memory Management, with its built-in garbage collection, is one of the language’s finest achiev ...
- ural1037 Memory Management
Memory Management Time limit: 2.0 secondMemory limit: 64 MB Background Don't you know that at school ...
随机推荐
- eslintrc配置翻译
{ "env": { "browser": true, "node": true, "commonjs": true } ...
- try-catch
try{ // 程序代码 }catch(异常类型1 异常的变量名1){ // 程序代码 }catch(异常类型2 异常的变量名2){ // 程序代码 }catch(异常类型2 异常的变量名2){ // ...
- 根据不同的ie的版本号,制定不同的方法
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- Appium之python API
webdriver contexts(self) 说明:返回多个会话内容 使用:driver.contexts current_context(self) 说明:返回单个会话的内容 使用:driver ...
- jQuery基础1
jQuery是轻量级的JavaScript库,jQuery 库位于一个 JavaScript 文件中,其中包含了所有的 jQuery 函数.更少的代码做更多的事. jQuery 可以选取某些元素并执行 ...
- Application.Run()和Form.Show()以及Form.ShowDialog()
ShowDialog()弹出模式化的窗体 Show()弹出非模式化的窗体 模式窗体,在关闭或隐藏前无法切换到主窗体. 非模式窗体,变换焦点使不必关闭窗体 总结:显示重要的信息,还是用模式窗体,如删除文 ...
- 怎么解决xp系统不能安装NET Framework4.0?
第一步: 如果是XP系统: 1.开始——运行——输入cmd——回车——在打开的窗口中输入net stop WuAuServ 2.开始——运行——输入%windir% 3.在打开的窗口中有个文件夹叫So ...
- img标签 加载FTP的图片 C#
好吧,我是菜鸟,这是我今天遇到的问题,什么也不会,得高人指点 1.使用FtpWebRequest下载图片,以流存贮 2.在ashx文件里面直接已流方式(HttpContext.Current.Resp ...
- rtc关机闹钟2 Alarm manager
public void set(int type, long triggerAtMillis, long windowMillis, long intervalMillis, PendingInten ...
- Berkeley DB的数据存储结构——哈希表(Hash Table)、B树(BTree)、队列(Queue)、记录号(Recno)
Berkeley DB的数据存储结构 BDB支持四种数据存储结构及相应算法,官方称为访问方法(Access Method),分别是哈希表(Hash Table).B树(BTree).队列(Queue) ...