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 ...
随机推荐
- ROS学习笔记(五)——建立工作空间
pre.ctl { font-family: "Liberation Mono", monospace } p { margin-bottom: 0.25cm; line-heig ...
- .net(C#)在vs2010版本下的MVC如何配置才能切换静态页面(html)
由于vs2010用的人比较多,虽然建mvc项目vs2010可能还不成熟,但鉴于每个人的成长有限,每个地方的资源有限,最主要的是为了解决问题,所以先不管那么多了. 用vs2010为公司网站建站,要求js ...
- 解决jquery操作checkbox全选全不选无法勾选问题
最近在学习中使用jquery操作checkbox,使用下面方法进行全选.反选:$("input[name='checkbox']").attr("checked" ...
- 分布式事务二阶提交DTS系统
前端时间写新交易系统时,经常碰到事务一致性问题,网上搜了一下,有一些解决方法,采用了扫表补偿的方式来完成,刚开始只有几个接口需要处理,工作量还可以,但是后续随着需求的增加,这些场景错综复杂,导致大量时 ...
- java网络编程,简单的客户端和服务器端
1.服务器端 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import ...
- unity调用摄像头的方法
http://blog.csdn.net/cocoa_china/article/details/10527995 using UnityEngine; using System.Collection ...
- openxml(二) 添加页眉,页脚
openxml 中 word 文档的结构是如下图: 其中,页眉是 header,属于headerpart 部件,页脚是footer,属于footerpart 部件,图上还有其他的东西,之后会一一介绍. ...
- Android的常用adb命令
第一部分:1. ubuntu下配置环境anroid变量:在终端执行 sudo gedit /etc/profile 打开文本编辑器,在最后追加#setandroid environment2. 运行E ...
- gulp教程之gulp-livereload
简介: gulp-livereload拯救F5!当监听文件发生变化时,浏览器自动刷新页面.[事实上也不全是完全刷新,例如修改css的时候,不是整个页面刷新,而是将修改的样式植入浏览器,非常方便.]特别 ...
- js 读取 地址栏参数 转
用JS获取地址栏参数的方法(超级简单) 方法一:采用正则表达式获取地址栏参数:( 强烈推荐,既实用又方便!) function GetQueryString(name) { var re ...