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 ...
随机推荐
- 【Java】异常处理_学习笔记
异常: 1.格式1: try { //业务代码 } catch(Exception e) { //异常处理代码 } 说明: a. 异常抛出:执行try里的代码,系统会自动生成一个异常对象,该对象会 ...
- 解决hibernate删除时的异常
由于关联关系是一对多和多对一的关系,于是在代码中需要删除多的一方的对象时出现了 deleted object would be re-saved by cascade (remove deleted ...
- 前端静态资源版本更新与缓存之——gulp自动化添加版本号
公司项目每次发布后,偶尔会有缓存问题,然后看了下gulp,发现gulp还能给js,css自动化添加版本号,可解决缓存的问题,所以自动化实现静态资源的版本更新才是正道.通过网上的资料试过了两种办法: 1 ...
- PHP JSON
- haligong2016
A 采用递推的方法,由于要到达棋盘上的一个点,只能从左边或者上边过来,根据加法原则,到达某一点的路径数目,就等于到达其相邻的上点和左点的路径数目的总和.所有海盗能达到的点将其路径数置为0即可. #in ...
- Unity脚本时间执行顺序
1.Awake Awake用于脚本唤醒.此方法为系统执行的第一个方法,用于脚本的初始化,在脚本的生命周期中只执行一次. 2.Start Start方法在Awake之后执行,在脚本生命周期中只执行一次. ...
- CSS3学习之分享下transition属性
最近在网上看到很多transition写的效果,借鉴http://www.w3school.com.cn分享下代码, 1.语法:transition: property duration timing ...
- PHP 常用函数的解释
1.trim() 去掉字符序列左边和右边的空格 2.stripslashes() 去掉反斜线字符 3.htmlspecialchars() 把预定义的字符 "<" (小于)和 ...
- sql sever读取Excel总结【转】
主要用到openrowset,opendatasource系统函数,这两个函数任意一个都能完成任务 用这种方法可以实现Excel和sqlserver表之间的相互导入导出. openrowset的写法 ...
- FZU 2150 Fire Game
Fire Game Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit St ...