自动释放池是OC里面的一种内存回收机制,一般可以将一些临时变量添加到自动释放池中,统一回收释放,当自动释放池销毁时,池里面的所有对象都会调用一次release,也就是计数器会减1,但是自动释放池被销毁了,里面的对象并不一定会被销毁。

OC对象发送一条autorelease消息,就会把这个对象添加到最近的自动释放池中也就是栈顶释放池中, Autorelease实际上是把对release的调用延迟了,对于每一次autorelease,系统只是把对象放入了当前的autorelease poo(栈顶释放池中)l中,当pool被释放时,pool中所有的对象都会被调用release。

----------------------------------------------------

Autorelease will not change the Object counter,just throw them into a pool.

When destroy the pool?

}

-------------------------------------------

Way of writing

1

main()

{

@autorelease{

Student *stu=[[Student alloc]init];

[stu autorelease];

Student *stu1=[[Student alloc]init];

[Stu1=autorelease];

}

return 0;

}

2           //Most common used

Student  *stu=[[[Student alloc]init]autorelease];

Student *stu1=[[[Student alloc]init]autorelease];

-------------------------------------------

//Create Autoreleasepool

1. iOS 5.0 after

@ autoreleasepool{

//code here

}

2.iOS 5.0 before

NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];

//code here

[pool release];

// Or [pool drain];  Only have difference in Mac development between release and //drain.

-------------------------------------------------

//Another clever use of autorelease

//Create a static method to  speedly create an object

//Student.h ( Method name is the same as class name by default)

//+(id)student;

//+(Student)student;

//Student.m

+(id)student{

Student *stu=[[[Student alloc]init]autorelease];

return stu;

}

main(){

autoreleasepool{

Student *stu=[Student student]

}

}

//latent rules

//iOS Static method/Object actually don't need developers to manage memory.

//They do autorelease inside themselves.

//So if you create a static method yourself ,you'd better guarantee it can autorelease.

-----------------------------------

//Create attribute for a class

//Student.h

@interface Student:NSObject

@property (nonatomic,assign) int age;

+(id)studentWithAge:(int)age;

@end

//Student.m

+(id)studentWithAge:(int)age{

Student *stu=[[[Student alloc]init]autorelease];

//Student *stu=[Student student];

//Student *stu=[self student]; //如何判别self指向谁?看谁调用studentWithAge

//静态方法,类名调用,当前调用这个方法的某个东西(对象,类)

//动态对象,(对象)

stu.age=age;

return stu;

}

---------------------------------------

questions

1 Final student method

+(id)student{

return [[[Student alloc]init]autorelease];

}

2

1.在ARC下,不能使用 [[ NSAutoreleasePool alloc ] init ](在5.0以前可以使用),而应该使用@autoreleasepool.(垃圾回收机制是完全不用我们释放内存,由开发工具来管理内存)

2.不要把大量循环放在autoreleasepool中,这样会造成内存峰值上升,因为里面创建的对象要等释放池销毁了才能释放,这种情况应该手动管理内存。(崩了好不)

3.尽量避免大内存使用该方法,对于这种延迟释放机制,尽量少用。(撑了好不)

4.SDK中利用静态方法创建并返回的对象都已经autorelease,不需要我们自己手动release。

但是通过[[NSNumber alloc]initWithInt:10]创建的对象需要release。

内存管理4-Autoreleasepool的更多相关文章

  1. 内存管理总结-autoreleasePool

    转自其他 序言 无论是在MRC时期还是ARC时期,做过开发的程序员都接触过autoreleasepool.尽管接触过但本人对它还不是很了解.本文只是将自己的理解说出来.在内存管理的文章中提到了OC的内 ...

  2. 菜鸟学习Cocos2d-x 3.x——内存管理

    菜鸟学习Cocos2d-x 3.x——内存管理 2014-12-10 分类:Cocos2d-x / 游戏开发 阅读(394) 评论(6)    亘古不变的东西 到现在,内存已经非常便宜,但是也不是可以 ...

  3. cocos2d-x内存管理

    Cocos2d-x内存管理 老师让我给班上同学讲讲cocos2d-x的内存管理,时间也不多,于是看了看源码,写了个提纲和大概思想 一.   为什么需要内存管理 1. new和delete 2. 堆上申 ...

  4. iOS开发系列—Objective-C之内存管理

    概述 我们知道在程序运行过程中要创建大量的对象,和其他高级语言类似,在ObjC中对象时存储在堆中的,系统并不会自动释放堆中的内存(注意基本类型是由系统自己管理的,放在栈上).如果一个对象创建并使用后没 ...

  5. ARC内存管理机制详解

    ARC在OC里面个人感觉又是一个高大上的牛词,在前面Objective-C中的内存管理部分提到了ARC内存管理机制,ARC是Automatic Reference Counting---自动引用计数. ...

  6. objective-c 语法快速过(6)内存管理原理

    内存管理基本原理(最重要) 移动设备的内存极其有限(iphone 4内存512M),每个app所能占用的内存是有限制的(几十兆而已). 当app所占用的内存较多时,系统会发出内存警告,这时得回收一些不 ...

  7. 06OC之内存管理

    在高级语言中,例如C#是通过垃圾回收机制(GC)来解决这个问题,但是在OC并没有类似的垃圾回收机制,因此必须由程序员手动去维护.今天就讲讲OC中的内存管理: 一.内存管理原理 在Xcode4.2之后的 ...

  8. iOS经典面试题总结--内存管理

    iOS经典面试题总结--内存管理 内存管理 1.什么是ARC? ARC是automatic reference counting自动引用计数,在程序编译时自动加入retain/release.在对象被 ...

  9. 【Cocos2d-x 3.x】内存管理机制与源码分析

    侯捷先生说过这么一句话 :  源码之前,了无秘密. 要了解Cocos2d-x的内存管理机制,就得阅读源码. 接触Cocos2d-x时, Cocos2d-x的最新版本已经到了3.2的时代,在学习Coco ...

  10. iOS开发ARC内存管理技术要点

    本文来源于我个人的ARC学习笔记,旨在通过简明扼要的方式总结出iOS开发中ARC(Automatic Reference Counting,自动引用计数)内存管理技术的要点,所以不会涉及全部细节.这篇 ...

随机推荐

  1. 怎样理解数组的空元素empty与undefined的区别

    数组的空元素empty表示空位, 它不是一种数据类型, 而是由于人为修改arr.length 或者写入时多写了逗号造成的. var arr = [1,2,3,4,,,5]; arr.length; a ...

  2. 怎样理解构造函数中的return语句

    因为构造函数也是一个函数, 自然也可以有return语句, 不过和一般函数不太一样的是, 在构造函数中如果return的是一个对象, 则会直接返回这个对象, 如果return 的不是一个对象, 那在n ...

  3. (六)发送、接收SOAP消息(1)

    一.为什么要用soap 原本我们使用web服务都是根据wsdl生成客户端(生成一堆java文件)然后再调用,本章节讲解如何用soap消息来替代这种方式. 二.SOAP消息格式 SOAP(简单对象访问协 ...

  4. 如何解决js地址栏中传递中文乱码的问题

    目标要求: 实现从A页面跳转至B页面,B页面接收A页面通过地址栏传递过来的中文参数,中文不能出现乱码. A页面部分代码(传递参数): var title = "这是中文"; var ...

  5. C++ 语句函数再探

    1. 表达式只计算,抛弃计算结果: 2. 空语句什么也不做: 3.switch case语句漏写break,将会从匹配到的情况开始执行,直到语句结束 int main() { ; i + ; //表达 ...

  6. Signal Processing and Pattern Recognition in Vision_15_RANSAC:Random Sample Consensus——1981

    此部分是 计算机视觉中的信号处理与模式识别 与其说是讲述,不如说是一些经典文章的罗列以及自己的简单点评.与前一个版本不同的是,这次把所有的文章按类别归了类,并且增加了很多文献.分类的时候并没有按照传统 ...

  7. 【2018-01-26】SqlServer 检查死锁和阻塞

    利用sys.sysprocesses SQL进程检查是否出现死锁和阻塞 Sys.SysProcesses 系统表是一个很重要的系统视图,主要用来定位与解决Sql Server的阻塞和死锁 select ...

  8. MyEclipse设置java文件每行字符数

    window->preferences->java->code style->formatter->edit->line wrapping->maximum ...

  9. 10_Azkaban案例实践3_Command操作HDFS

    HDFS操作任务 1.创建job描述文件 # fs.job type=command command=/usr/local/src/hadoop-2.6.4/bin/hadoop fs -mkdir ...

  10. docker在Linux环境下的安装

    在Centos6.8上安装 一.查看系统版本 二.安装EPEL 因为系统自带的repo中不带docker需要安装epel rpm -Uvh http://dl.fedoraproject.org/pu ...