自动释放池是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. ELK 日志收集系统

    传统系统日志收集的问题 在传统项目中,如果在生产环境中,有多台不同的服务器集群,如果生产环境需要通过日志定位项目的Bug的话,需要在每台节点上使用传统的命令方式查询,这样效率非常底下. 通常,日志被分 ...

  2. android 蓝牙连接端(客户端)封装

    0.权限  AndroidManifest.xml <uses-permission android:name="android.permission.BLUETOOTH"/ ...

  3. SOLID Principles

    Intention: more understandable, easier to maintain and easier to extend.(通过良好的设计使得代码easy and simple, ...

  4. 34. Find First and Last Position of Element in Sorted Array + 二分

    题意懒得抄了,大概是:在升序数组中给定整数target,找到第一个和最后一个target的索引,找到返回{index1, index2},否则返回{-1, -1}: 时间复杂度要求:O(logn) 分 ...

  5. linux:# vi /etc/profile -bash: vi: command not found 的解决办法

    /bin/vi /etc/profile 直接用全路径vi,linux下一切皆文件,进去把profile文件内容改一下,一定是profile出了问题 export JAVA_HOME=/usr/jav ...

  6. Java知识导航总图

    1.系统构架 企业服务总线(ESB).微服务.面向服务的架构(SOA) 了解分布式文件存储系统,掌握集群化开发及部署 2.系统系统集成技术 Wsbservice.Socket 3.RPC远程调用的相关 ...

  7. python自定义小工具:密码匿名化、毫秒时间显示、人类易读字节

    import base64 import time def timestamp2datems(timestamp): ''' 时间戳转为日期字串,精确到ms.单位s :param timestamp: ...

  8. 个人作业-Alpha项目测试—luomei1547

    这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1/ 这个作业要求在哪里 https://edu.cnbl ...

  9. 在 java 程序中怎么保证多线程的运行安全?(未完成)

    在 java 程序中怎么保证多线程的运行安全?(未完成)

  10. Spring-使用JAVA的方式配置Spring-代理模式

    9.使用Java的方式配置Spring 我们现在要完全不使用Spring的xml配置了,全权交给Java来做! JavaConfig是Spring的一个子项目,在Spring4之后,它成为了一个核心功 ...