Objective -C Memory Management 内存管理 第一部分
Objective -C Memory Management 内存管理 第一部分
Memory management is part of a more general problem in programming called resource management.
内存管理是资源管理的一部分。
Every computer system has finite resources for your program to use. These include memory, open files, and network connections. If you use a resource, such as by opening a file, you need to clean up after yourself (in this case, by closing the file).
每个电脑资源有限。包括内存,打开文件数,网络连接。如果你打开了一个文件,就应该清理掉。
Our friends in
the Java and scripting worlds have it easy: memory management happens automatically for them, like having their parents clean up their rooms.
java 和它的脚本世界很容易:内存管理对他们是自动的。就像父亲收拾孩子的房间一样。
If we allocate without freeing, we'll leak memory: our program's memory consumption will grow and grow until we run out of memory, and then, the program will crash.
如果分配了内存,却没有回收,那么可能导致内存泄露:我们的程序占用越来越多的内存,直至用完内存,程序崩溃。
1.1 Object Life Cycle 对象声明周期
Just like the birds and the bees out here in the real world, objects inside a program have a life cycle. They're born (via alloc or new); they live (receive messages and do stuff), make friends (via composition and arguments to methods), and eventually die (get freed) when their lives are over. When that happens, their raw materials (memory) are recycled and used for the next generation.
就像现实世界的鸟和蜜蜂一样,在一个程序内部的对象也有生命周期。他们诞生(通过 alloc 和new) ,他们生活(接受消息和东西),他们交朋友(通过组合和参数方法)并最终死去。这样他们原来的材料(内存)被回收再利用。 说的和人一样啊啊啊。
1.2 Reference Counting
Cocoa uses a technique known as reference counting, also sometimes called retain counting.
利用引用计数或者保留计数。
Every object has an integer associated with it, known as its reference count or retain count.
每个对象都有自己的引用计数
When some chunk of code is interested in an object, the code increases the object's retain count, saying, "I am interested in this object." When that code is done with the object, it decreases the retain count, indicating that it has lost interest in that object.
有代码对一个对象感兴趣,就增加retain count。如果处理完了,就减少retain count。
When the retain count goes to 0, nobody cares about the object anymore (poor object!), so it is destroyed and its memory is returned to the system for reuse.
当retain count 是0时,就等着被回收或处理吧。
When an object is about to be destroyed because its retain count has reached 0, Objective-C automatically sends the object a dealloc message.
如果一个对象将被销毁因为retain count 到达了0
To find out the current retain count, send the retainCount message. Here are the signatures for retain, release and retainCount:
- (id) retain;
- (oneway void) release;
- (NSUInteger) retainCount;
A retain call returns an id. This enables you to chain a retain call with other message sends, incrementing the object's retain count and then asking it to do some work. For instance, [[car retain] setTire: tire atIndex: 2]; asks car to bump up its retain count and perform the setTire action.
一个retain 调用能增加retain count,然后要求它做一些工作。
1.3 Object ownership
When something is said to "own an object," that something is responsible for making sure the object gets cleaned up.
当你说你拥有这个对象的时候,那么你也要为他擦屁股。:-)
1.4 Retaining and releasing in accessor
- (void) setEngine: (Engine *) newEngine
{
engine = [newEngine retain];
// BAD CODE: do not steal. See fixed version below.
} // setEngine
Engine *engine1 = [Engine new]; // count: 1
[car setEngine: engine1]; // count: 2
[engine1 release]; // count: 1
Engine *engine2 = [Engine new]; // count: 1
[car setEngine: engine2]; // count: 2
Oops! We have a problem with engine1 now: its retain count is still 1.
engine1 的retain count 仍然是1.main()已经释放了它对engine1的索引,但是Car不会。
Here's another attempt at writing setEngine:.
另外一种形式的setEngine方法
- (void) setEngine: (Engine *) newEngine
{
[engine release];
engine = [newEngine retain];
// More BAD CODE: do not steal. Fixed version below.
} // setEngine
Engine *engine = [Engine new]; // count: 1
Car *car1 = [Car new];
Car *car2 = [Car new];
[car1 setEngine: engine]; // count: 2
[engine release]; // count 1
[car2 setEngine: [car1 engine]]; // oops!
[car1 engine] returns a pointer to engine, which has a retain count of 1. The first line of setEngine is [engine release], which makes the retain count 0, and the object gets deallocated.
[car1 engine]返回一个指针到engine,它的retain count 是1 。而setEngine 的第一行是release engine 。因此retain count 是0.因此对象被重新分配。
Here's a better way to write setEngine:
这个setEngine 比较好:
- (void) setEngine: (Engine *) newEngine
{
[newEngine retain];
[engine release];
engine = newEngine;
} // setEngine
In your accessors, if you retain the new object before you release the old object, you'll be safe.
在存储中,如果你保留新的对象在你释放久的对象之前。那么你就安全了。
1.5Autorelease 自动释放
cocoa has the concept of the autorelease pool .
cocoa 有自动释放池的概念。
The name provides a good clue.
It's a pool (collection) of stuff, presumably objects, that automatically gets released.
从名字可以看出它大概是自动释放的东西。
NSObject provides a method called autorelease:
- (id) autorelease;
This method schedules a release message to be sent at some time in the future. The return value is the object that receives the message; retain uses this same technique, which makes chaining together calls easy. When you send autorelease to an object, that object is actually added to
an autorelease pool. When that pool is destroyed, all the objects in the pool are sent a release message.
这个方法计划了一个释放信号被送出。 当你发出autorelease 给一个对象,该对象将加入autorelease pool .当pool 被释放,所有在这个pool中得对象将被发出释放信息。
- (NSString *) description
{
NSString *description;
description = [[NSString alloc]
initWithFormat: @"I am %d years old", 4];
return ([description autorelease]);
} // description
So you can write code like this:
NSLog (@"%@", [someObject description]);
1.6 销毁的前夕 The Eve of Our Destruction
When does the autorelease pool get destroyed so that it can send a release message to all the objects it contains? For that matter, when does a pool get created in the first place?
什么时候自动释放池被销毁,什么时候资源池被创建?
There are two ways you can create an autorelease pool.
有两种方式创建:
(1)Using the @autoreleasepool language keyword.
(2)Using the NSAutoreleasePool object.
第一种:@ autoreleasepool{} 。大括号里面的将放到新的pool中。
The second, and more explicit, method is to use the NSAutoreleasePool object. When you do this, the code between new and release gets to use the new pool.
第二种,用NSAutoreleasePool
NSAutoreleasePool *pool;
pool = [NSAutoreleasePool new];
...
[pool release];
int main (int argc, const char *argv[])
{
NSAutoreleasePool *pool;
pool = [[NSAutoreleasePool alloc] init];
RetainTracker *tracker;
tracker = [RetainTracker new]; // count: 1
[tracker retain]; // count: 2
[tracker autorelease]; // count: still 2
[tracker release]; // count: 1
NSLog (@"releasing pool");
[pool release];
// gets nuked, sends release to tracker
@autoreleasepool
{
RetainTracker *tracker2;
tracker2 = [RetainTracker new]; // count: 1
[tracker2 retain]; // count: 2
[tracker2 autorelease]; // count: still 2
[tracker2 release]; // count: 1
NSLog (@"auto releasing pool");
}
return (0);
} // main
[tracker autorelease]; // count: still 2
Then the object gets autoreleased. Its retain count is unchanged: it's still 2. The important thing to note is that the pool that was created earlier now has a reference to this object.
这个对象获得自动释放了。它的retain count 仍是2。但重要的是pool 有这个对象的一个reference了。
Objective -C Memory Management 内存管理 第一部分的更多相关文章
- Objective-C Memory Management 内存管理 2
Objective-C Memory Management 内存管理 2 2.1 The Rules of Cocoa Memory Management 内存管理规则 (1)When you c ...
- [Android Memory] Android内存管理、监测剖析
转载自:http://blog.csdn.net/anlegor/article/details/23398785 Android内存管理机制: Android内存管理主要有:LowMemory Ki ...
- Objective-C(内存管理)
引用计数器 每个OC对象都有一个占4个字节存储空间的引用计数器 当使用或创建一个对象时,新对象的引用计数器默认是1 retain:可以使引用计数器+1 release:可以是引用计数器-1 retai ...
- IOS学习笔记3—Objective C—简单的内存管理
今天简述一下简单的内存管理,在IOS5.0以后Apple增加了ARC机制(Automatic Reference Counting),给开发人员带来了不少的方便,但是为了能更好的理解IOS内存管理机制 ...
- 【译】x86程序员手册13-第5章 内存管理
Chapter 5 Memory Management 内存管理 The 80386 transforms logical addresses (i.e., addresses as viewed b ...
- innodb源码解析 - mem0_.c - 基本内存管理
The basic element of the memory management is called a memoryheap. A memory heap is conceptually ast ...
- ORACLE 11G内存管理方式
SGA包含的组件: 组件名 说明 参数 buffer cache 存放从数据文件中读取的数据拷贝,所有用户之间是可以共享的 db_cache_size db_keep_cache_size db_re ...
- iOS内存管理部分内容
Objective-C 高级编程 iOS与OS X多线程和内存管理第一章部分讲述了关于ARC的内容,还讲述了关于修饰符的问题,还讲了好多底层的实现的内容,这些底层实现却往往是在面试的过程中经常被遇到的 ...
- Flink内存管理源代码解读之基础数据结构
概述 在分布式实时计算领域,怎样让框架/引擎足够高效地在内存中存取.处理海量数据是一个非常棘手的问题.在应对这一问题上Flink无疑是做得非常杰出的,Flink的自主内存管理设计或许比它自身的知名度更 ...
随机推荐
- LIKIE INSTR
SELECT url FROM test_url WHERE FROM_UNIXTIME(create_time,'%Y%m%d %H') < '20171218 00' AND no ...
- jetty与tomcat
相同点: 1.tomcat与jetty都是一种servlet引擎,他们都支持标准的servlet规范和javaEE规范 不同点: 1.架构比较 jetty相比tomcat更为简单 jetty架构是基于 ...
- 【转】Google 发布 Android 性能优化典范(比较老,但很实用)
2015年伊始,Google发布了关于Android性能优化典范的专题, 一共16个短视频,每个3-5分钟,帮助开发者创建更快更优秀的Android App.课程专题不仅仅介绍了Android系统中有 ...
- bzoj 1941 Hide and Seek
题目大意: n个点,求每个点到其最远点距离-到其最近点距离(除自己之外)的最小值 思路: 对于估计函数的理解还不够深刻 #include<iostream> #include<cst ...
- python解决list unicode转中文显示
#!/usr/bin/python# #-*-coding:UTF-8-*- import xlrd book = xlrd.open_workbook('Interface_data.xlsx') ...
- 使用x-template 定义模板
demo <script type="text/x-template" id="myFirstScriptComponent"> <p> ...
- 第十七周 - OpenCV 学习笔记 S1 - OpenCV 基本函数
Imread()函数: 基本功能:读取图像到OpenCv中. 1.函数原型: Mat imwrite(const strings& filename, int flag = 1); 第一个参数 ...
- 转:IIS MVC 发布错误 403.14-Forbidden Web 服务器被配置为不列出此目录的内容
访问网址:http://blog.csdn.net/csethcrm/article/details/37820135 有两个地方需要配置: 1.web.config中的节点: <system. ...
- 机器学习--DIY笔记与感悟--①K-临近算法
##“计算机出身要紧跟潮流” 机器学习作为如今发展的趋势需要被我们所掌握.而今我也需要开始learn机器学习,并将之后的所作所想记录在此. 今天我开始第一课--K临近算法. 一.k-临近的基础概念理解 ...
- bzoj 1055: [HAOI2008]玩具取名【区间dp】
不难想,就是处理起来比较麻烦 设f[i][j][k]为是否可以把区间(i,j)合并为k,初始状态是f[i][j][s[i]]=1,转移的话另一段枚举长度x,向(i-x,j),(i,j+x)转移 把四个 ...