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 object

    Bar 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的更多相关文章

  1. Objective -C Memory Management 内存管理 第一部分

    Objective -C Memory Management  内存管理  第一部分 Memory management is part of a more general problem in pr ...

  2. Memory Management in Open Cascade

    Open Cascade中的内存管理 Memory Management in Open Cascade eryar@163.com 一.C++中的内存管理 Memory Management in ...

  3. Java (JVM) Memory Model – Memory Management in Java

    原文地址:http://www.journaldev.com/2856/java-jvm-memory-model-memory-management-in-java Understanding JV ...

  4. Operating System Memory Management、Page Fault Exception、Cache Replacement Strategy Learning、LRU Algorithm

    目录 . 引言 . 页表 . 结构化内存管理 . 物理内存的管理 . SLAB分配器 . 处理器高速缓存和TLB控制 . 内存管理的概念 . 内存覆盖与内存交换 . 内存连续分配管理方式 . 内存非连 ...

  5. 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- ...

  6. 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 ...

  7. Understanding Memory Management(2)

    Understanding Memory Management Memory management is the process of allocating new objects and remov ...

  8. Java Memory Management(1)

    Java Memory Management, with its built-in garbage collection, is one of the language’s finest achiev ...

  9. ural1037 Memory Management

    Memory Management Time limit: 2.0 secondMemory limit: 64 MB Background Don't you know that at school ...

随机推荐

  1. C#socket通信-----多线程

    我在之前的socket通信的基础上做了一点改进,使用多线程来使用,程序更加简洁实用.不足之处请指教哦! 话不多说,之前的随笔也有介绍,直接上代码啦! 服务端socket(serverSocket): ...

  2. 转:深入浅出UML类图(具体到代码层次)

    深入浅出UML类图 作者:刘伟 ,发布于:2012-11-23,来源:CSDN   在UML 2.0的13种图形中,类图是使用频率最高的UML图之一.Martin Fowler在其著作<UML ...

  3. mysql操作查询结果case when then else end用法举例

    Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex          WHEN '1' THEN '男'          WHEN '2' THEN ...

  4. Michael Schatz - 序列比对课程

    Michael Schatz - Cold Spring Harbor Laboratory 最近在研究 BWA mem 序列比对算法,直接去看论文,看不懂,论文就3页,太精简了,好多背景知识都不了解 ...

  5. windows下安装python模块

    如何在windows下安装python模块 1. 官网下载安装包,比如(pip : https://pypi.python.org/pypi/pip#downloads) pip-9.0.1.tar. ...

  6. C# GDI+发生一般性错误(A generic error occurred in GDI+))

    解决思路: 1. 因为 .net GDI+ 是对底层 的封装. 所以可以尝试用 Marshal.GetLastWin32Error();函数获得底层错误代码. try{ image.Save(file ...

  7. swift_Class类的继承

    //: Playground - noun: a place where people can play var str = "Hello, playground" //***** ...

  8. js创建标签的方法--依赖于jquery

    /** * 创建标签,传入一个对象,返回一个完整的标签 * @param {Object.attribute} tag 标签 * @param {Object.attribute} attribute ...

  9. 采用flask+uwsgi+nginx架构将flask应用程序部署在腾讯云

    最近一星期加班为学校做了一个教师发展中心平台,在此总结一下部署经验 环境:Centos7.0  python2.7.5 1.安装nginx 命令行输入指令:sudo yum install nginx ...

  10. Oracle 11g default profile 默认启用密码过期180天 ORA-28001错误处理

    问题描述:客户反映客户端不能登录应用程序时不能连接.环境:AIX 5.3 + Oracle 11gR2解决:远程Telnet到数据库主机,sqlplus / as sysdba 连接后操作正常,表明数 ...