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. Connection broken for id 62, my id = 70, error =

    启动费zokeeper失败,报错如下:Connection broken for id 62, my id = 70, error = 原因是因为zoo.cfg中server.id不正确. serve ...

  2. HDFS 核心原理

    HDFS 核心原理 2016-01-11 杜亦舒 HDFS(Hadoop Distribute File System)是一个分布式文件系统文件系统是操作系统提供的磁盘空间管理服务,只需要我们指定把文 ...

  3. CI 3.0.6 控制器打印base_url 地址不为 localhost的解决方法

    1.在application\config\autoload.php 第92行 加载url    $autoload['helper'] = array('url'); 2.application\c ...

  4. 如何让同局域网的同事访问我电脑上的PHP网站和数据库

    需求:想让公司同一局域网的同事电脑访问我的电脑里面的php项目. 条件:首先确认localhost正常访问你的本地项目 环境:我使用的是wampserver2.5集成环境 步骤: 1.增加新增监听端口 ...

  5. react.js

    注释:      React JSX需要的注释格式是:      {/*....*/}      WebStorm默认的是:      /*.....*/   作为子节点 {/*...*/} 作为内联 ...

  6. SQL server 常用语句

    SQL Server中常用的SQL语句   1.概述 2.查询概述 3.单表查询 4.连接查询 5.带有exists的相关子查询 6.SQL的集合操作 7.插入操作 8.删除操作 9.修改操作 10. ...

  7. node.js的优缺点

    node.js的优缺点 优点: 1. 采用事件驱动,异步编程,为网络服务而设计. 2. node.js非阻塞模式的IO处理给node.js带来在相对较低的资源耗用下的高性能与出众的负载能力. 3. n ...

  8. Microsoft Office Project 相关教程 收集

    Project教程 如何建立任务间链接 Project教程:[10]如何将项目插入主项目 如何有效使用Project(1)——编制进度计划.保存基准 如何有效使用Project(2)——进度计划的执行 ...

  9. [MISSAJJ原创]cell内 通过SDWebImage自定义创建动态菊花加载指示器

    最后更新已经放到了github上了 MISSAJJ自己写的一个基于SDWebImage自定义的管理网络图片加载的工具类(普通图片加载,渐现Alpha图片加载,菊花Indicator动画加载) 经常在项 ...

  10. Ubuntu 下 kazam 录屏 没声音解决方案

    以下内容参考https://www.youtube.com/watch?v=5NZ0qwp2L04,我做了些修改,让它好懂些. 在应用商店里搜索 PulseAudio Volume Control 在 ...