NSAutoreleasePool & thread
https://developer.apple.com/documentation/foundation/nsautoreleasepool
An object that supports Cocoa’s reference-counted memory management system.
An autorelease pool stores objects that are sent a release
message when the pool itself is drained.
Important
If you use Automatic Reference Counting (ARC), you cannot use autorelease pools directly. Instead, you use @autoreleasepool
blocks. For example, in place of:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Code benefitting from a local autorelease pool.
[pool release];
you would write:
@autoreleasepool {
// Code benefitting from a local autorelease pool.
}
@autoreleasepool
blocks are more efficient than using an instance of NSAutoreleasePool
directly; you can also use them even if you do not use ARC.
In a reference-counted environment (as opposed to one which uses garbage collection), an NSAutoreleasePool
object contains objects that have received an autorelease
message and when drained it sends a release
message to each of those objects. Thus, sending autorelease
instead of release
to an object extends the lifetime of that object at least until the pool itself is drained (it may be longer if the object is subsequently retained). An object can be put into the same pool several times, in which case it receives a release
message for each time it was put into the pool.
In a reference counted environment, Cocoa expects there to be an autorelease pool always available. If a pool is not available, autoreleased objects do not get released and you leak memory. In this situation, your program will typically log suitable warning messages.
The Application Kit creates an autorelease pool on the main thread at the beginning of every cycle of the event loop, and drains it at the end, thereby releasing any autoreleased objects generated while processing an event. If you use the Application Kit, you therefore typically don’t have to create your own pools. If your application creates a lot of temporary autoreleased objects within the event loop, however, it may be beneficial to create “local” autorelease pools to help to minimize the peak memory footprint.
You create an NSAutoreleasePool
object with the usual alloc
and init
messages and dispose of it with drain
(or release
—to understand the difference, see Garbage Collection). Since you cannot retain an autorelease pool (or autorelease it—see retain
and autorelease
), draining a pool ultimately has the effect of deallocating it. You should always drain an autorelease pool in the same context (invocation of a method or function, or body of a loop) that it was created. See Using Autorelease Pool Blocks for more details.
Each thread (including the main thread) maintains its own stack of NSAutoreleasePool
objects (see Threads). As new pools are created, they get added to the top of the stack. When pools are deallocated, they are removed from the stack. Autoreleased objects are placed into the top autorelease pool for the current thread. When a thread terminates, it automatically drains all of the autorelease pools associated with itself.
Threads
If you are making Cocoa calls outside of the Application Kit’s main thread—for example if you create a Foundation-only application or if you detach a thread—you need to create your own autorelease pool.
If your application or thread is long-lived and potentially generates a lot of autoreleased objects, you should periodically drain and create autorelease pools (like the Application Kit does on the main thread); otherwise, autoreleased objects accumulate and your memory footprint grows. If, however, your detached thread does not make Cocoa calls, you do not need to create an autorelease pool.
Note
If you are creating secondary threads using the POSIX thread APIs instead of NSThread
objects, you cannot use Cocoa, including NSAutoreleasePool
, unless Cocoa is in multithreading mode. Cocoa enters multithreading mode only after detaching its first NSThread
object. To use Cocoa on secondary POSIX threads, your application must first detach at least one NSThread
object, which can immediately exit. You can test whether Cocoa is in multithreading mode with the NSThread
class method isMultiThreaded
.
Garbage Collection
In a garbage-collected environment, there is no need for autorelease pools. You may, however, write a framework that is designed to work in both a garbage-collected and reference-counted environment. In this case, you can use autorelease pools to hint to the collector that collection may be appropriate. In a garbage-collected environment, sending a drain
message to a pool triggers garbage collection if necessary; release
, however, is a no-op. In a reference-counted environment, drain
has the same effect as release
. Typically, therefore, you should use drain
instead of release
.
NSAutoreleasePool & thread的更多相关文章
- NSAutoReleasePool
做iPhone应用开发已经2年多了, 但一些基础的概念性问题只是大致了解, 脑袋中有个模糊的概念. 虽然对平时工作开发没什么影响, 不过时间长了, 心里总是有点虚. 所以从现在开始, 每当我遇到一个模 ...
- 63 (OC)* NSAutoreleasePool 自动释放池
目录 0:ARC 1: 自动释放池 2:NSAutoreleasePool实现原理 3:autorelease 方法 4: Runloop和Autorelease的关系 5: Using Autore ...
- 多线程爬坑之路-Thread和Runable源码解析之基本方法的运用实例
前面的文章:多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类) 多线程爬坑之路-Thread和Runable源码解析 前面 ...
- 记一次tomcat线程创建异常调优:unable to create new native thread
测试在进行一次性能测试的时候发现并发300个请求时出现了下面的异常: HTTP Status 500 - Handler processing failed; nested exception is ...
- 多线程爬坑之路-Thread和Runable源码解析
多线程:(百度百科借一波定义) 多线程(英语:multithreading),是指从软件或者硬件上实现多个线程并发执行的技术.具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程,进而提 ...
- Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
学习架构探险,从零开始写Java Web框架时,在学习到springAOP时遇到一个异常: "C:\Program Files\Java\jdk1.7.0_40\bin\java" ...
- Exception in thread "main" java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(I)V
在学习CGlib动态代理时,遇到如下错误: Exception in thread "main" java.lang.NoSuchMethodError: org.objectwe ...
- Thread.Sleep(0) vs Sleep(1) vs Yeild
本文将要提到的线程及其相关内容,均是指 Windows 操作系统中的线程,不涉及其它操作系统. 文章索引 核心概念 Thread.Yeild Thread.Sleep(0) Thread. ...
- Android笔记——Handler Runnable与Thread的区别
在java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnable接口:Thread类是在java.lang包中定义的.一个类只要继承了Thread类同时覆写了本类中的run() ...
随机推荐
- 数组API(2)
ES5中数组新增API: 1. indexOf 作用:检测当前数据是否在数组中存在: 特点:如果存在返回相对应的下标,如果不存在则返回-1: 如果数组中存在多个需要判断的这个数据,则返回最近的这个数据 ...
- 关于状态压缩DP以及状态压缩
首先要明确:状态压缩是利用数字来代表一组序列的方法,从而降低序列访问的复杂度,本质上跟HASH有着差不多的思想,但是其实就是数位运算的一种 定义:集合中共有N个数字,其中每个数字均小于K,能么我们可以 ...
- AT2044 Teleporter
传送门 这个是真的简单题,随便手玩一下就可以发现最优策略一定是给\(1\)加上自环 然后就可以dfs一下看哪些点子树里深度最深的点到当前点的距离会等于\(k-1\),然后将当前点连向\(1\)(当然特 ...
- Jmeter3.2源码编译环境搭建(转)
1.下载jmeter3.2源码 https://github.com/apache/jmeter/tree/v3_2 https://blog.csdn.net/fly_to_higher/artic ...
- 1、python简单介绍
写在前面:曾经与java擦肩而过,现在懊悔很深,希望自己通过学习python,熟练掌握python,来弥补曾经的愚蠢.python简单介绍 python 1989年年底诞生,截止2017年,已经是IT ...
- 036 Valid Sudoku 有效的数独
详见:https://leetcode.com/problems/valid-sudoku/description/ class Solution { public: bool isValidSudo ...
- 类成员函数的重载、覆盖和隐藏区别 (C++)(转)
类成员函数的重载.覆盖和隐藏区别 (C++) 这是本人第一次写博客,主要是想记录自己的学习过程.心得体会,一是可以方便以后回顾相关知识,二是可以与大家相互学习交流. 关于C++中类成员函数的重载. ...
- 【JavaEE】WebService到底是什么?
一.序言 大家或多或少都听过WebService(Web服务),有一段时间很多计算机期刊.书籍和网站都大肆的提及和宣传WebService技术,其中不乏很多吹嘘和做广告的成分.但是不得不承认的是Web ...
- webservice初识,SOAP1.1版本
客户端与服务端模式,非web端发布 1.1 [Jax-ws第一个例子] 1.1.1 第一步:服务端开发 编写SEI(Service Endpoint Interface),SEI在w ...
- php面试题分享
1.nginx使用哪种网络协议? nginx是应用层 我觉得从下往上的话 传输层用的是tcp/ip 应用层用的是http fastcgi负责调度进程 2. <? echo 'hello tush ...