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. 关于STM32的抢占式优先级说明。——Arvin

    关于STM32的中断设置.--Arvin 中断 STM32 很多人在配置STM32中断时对固件库中的这个函数NVIC_PriorityGroupConfig()配置优先级分组方式,会很不理解,尤其是看 ...

  2. stm32定时器中断类型分析

    一直在用的stm32定时器的中断都是TIM_IT_Update更新中断,也没问为什么,直到碰到有人使用TIM_IT_CC1中断,才想到这定时器的中断类型究竟有什么区别,都怪当时学习stm32的时候不够 ...

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

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

  4. swiper横向轮播(兼容IE8)

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  5. select下拉框插件(转)

    <!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. text-transform属性

    text-transform -- 定义文本的大小写状态,此属性对中文无意义 取值:capitalize | uppercase | lowercase | none | inherit capita ...

  7. 并列统计CASE WHEN

    select sum(case when depart = 'Physical' then 1 else 0 end) PhyTotal, sum(case when depart = 'Chemis ...

  8. CSS创造三角形的原理

    其实就是利用了div各方向border的接驳点产生的斜线的特点,知道原理后就不觉得有多不可思议了.. .triangle_up { height: 0px; width: 0px; border-bo ...

  9. SPI 驱动分析

    断更博客两个月后我又回来了,眯着躺倒就能睡熟的小眼睛,在这儿敲键盘.这篇文章给你快乐,你有没有爱上我! SPI驱动由三部分组成,分别为drivers.core.device.通过bus总线连接.困了不 ...

  10. Python笔记总结week4

      1. Built-in functions 函数可能遇到的问题:下面例子函数改变了函数需要传入的参数 li = [11,22,33,44] def f1(arg): arg.append(55) ...