35 - Memory Management

1. Memory management
The role of memory: to store data.
1). How to store data in memory.
Declare a variable. Then store the data in it.
2). How the occupied memory space is released when the data is no longer used.
2. Five major regions in memory
Stack: Local variable. When the scope of the local variable is executed, the local variable will be reclaimed by the system immediately.
Heap: OC object. The space allocated by the C function.
BSS segment: Uninitialized global variables, static variables. Once initialized, they are recycled and dumped into the data segment.
Data segment: Global variables and static variables that have been initialized. They will not be recycled until the end of the program.
Code segment: code. When the program ends, the system will automatically reclaim the data stored in the code segment.
The collection of stack, BSS segment, data segment, and code segment data stored in them is done automatically by the system. We do not need to intervene.
3. OC objects allocated in the heap area definitely need to be recycled.
iPhone memory mechanism.
40M warning
45M warning
120M flashback.
OC objects stored in the heap will not be automatically reclaimed by the system. They will not be reclaimed until the end of the program.
4. Scope of memory management:
Only need to manage the collection of OC objects stored in the heap. The collection of data in other areas is automatically managed by the system.
5. When should objects be recycled?
When someone uses this object, this object must not be recycled.
This object can only be recycled when no one is using it.
6. Reference Counter
1). Each object has an attribute. It is called retainCount. It is called a reference counter. The type is unsigned long and occupies 8 bytes.
The role of the reference counter: It is used to record how many people are currently using this object.
By default, an object is created and the default value of the object's reference counter is 1.
2). When one more person uses the object. The value of the reference counter of the object should be +1 to represent the object is used by one more person.
3). 当这个对象少1个人使用的时候.应该先让这个对象的引用计数器的值-1 代表这个对象少1个人使用.
4). 当这个对象的引用计数器变为0的时候.代表这个对象无人使用. 这个时候系统就会自动回收这个对象.
7. 如何操作引用计数器.
1). 为对象发送1条retain消息. 对象的引用计数器就会加1. 当多1个人使用对象的时候才发.
2). 为对象发送1条release消息.对象的引用计数器就会减1. 当少1个人使用对象的时候才发.
3). 为对象发送1条retainCount消息. 就可以去到对象的引用计数器的值.
就这样++ -- 当对象的引用计数器变为0的时候,对象就会被系统立即回收.
在对象被回收的时候.会自动调用对象的dealloc方法.
8. 内存管理的分类
MRC: Manual Reference Counting 手动引用计数.手动内存管理.
当多1个人使用对象的时候,要求程序员手动的发送retain消息.少1个人使用的时候程序员手动的发送relase消息.
2011年之前 iOS5之前
ARC: Automatic Reference Counting 自动引用计数.自动内存管理.
系统自动的在合适的地方发送retain relase消息.
我们今天学习的MRC.
学习MRC的理由:
1). 面试必考 100%
2). 早期的APP开发使用的MRC技术.
3). iOS大牛都是从MRC成长起来的. 方便交流.
4). ARC是基于MRC

1. iOS5开始. Xcode4.2开始就支持ARC
Xcode7 默认支持ARC开发.
默认使用的开发方式就是ARC的模式.
关闭ARC开启MRC.
2. 当对象的引用计数器变为0的时候,系统会自动回收对象.
在系统回收对象的时候.会自动的调用对象的dealloc方法.
重写dealloc方法的规范:
必须要调用父类的dealloc方法. 并且要放在最后一句代码.
3. 测试引用计数器.
1). 新创建1个对象,这个对象的引用计数器的值默认是1.
2). 当对象的引用计数器变为0的时候.对象就会被系统立即回收 并自动调用dealloc方法.
3). 为对象发送retain消息 对象的引用计数器就会+1
4. 为对象发送release消息.并不是回收对象.而是让对象的引用计数器-1
当对象的引用计数器的值变为0的时候.对象才会被系统立即回收.

1. 内存管理的重点
1). 什么时候为对象发送retain消息.
当多1个人使用这个对象的时候,应该先为这个对象发送retain消息.
2). 什么时候为对象发送releaee消息.
当少1个人使用这个对象的时候.应该为这个对象发送1条release消息.
2. 在ARC机制下,retain release dealloc这些方法方法无法调用.
2. 内存管理的原则
1). 有对象的创建,就要匹配1个release
2). retain的次数和release的次数要匹配.
3). 谁用谁retain. 谁不用谁release.
谁负责retain 谁就负责relase
4). 只有在多1个人用的时候才retain 少1个人使用的时候才release
有始有终,有加就有减. 有retain就应该匹配1个release 一定要平衡.




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325926192&siteId=291194637