I. Introduction to memory Management
1. Why should I have memory management?
Mobile device memory is extremely limited, and each app can occupy a limited amount of memory.
The following behavior will consume memory
. Create an OC Object
. Define a variable
. Call a function or method
When the app takes up a lot of memory, the system issues a memory warning, and you need to reclaim some memory that is no longer in use.
If the app occupies too much memory, the system may force the app to shut down, causing a flashback and impacting the user experience
2. What is memory management
1> so-called memory management, is the memory management, involves the operation is:
Allocating memory: Creating an object, for example, increases memory consumption
. Clean up Memory: for example, destroying an object can reduce memory consumption
2> Management scope of memory management
. Any object that inherits the NSObject
Invalid for other non-object types (base data type/struct, etc.)
3> The essential reason why memory management is only required for OC objects
. OC objects stored inside the heap
Non-OC objects are generally placed inside the stack (stack memory is automatically reclaimed by the system)
3. Heap and Stack
1> stack (operating system): automatically allocated by the operating system release, storage function parameter value, local variable value, etc., its operation method is similar to the data structure in the stack (advanced post-out);
2> Heap (operating system): Usually released by the programmer, if the programmer does not release, the end of the program may have an OS recycle, distribution is similar to the list.
3> Example
int main(int argc, const char * argv[])
{
@autoreleasepool {
int a = 10; // 栈
int b = 20; // 栈
// p : 栈
// Person对象(计数器==1) : 堆
Person *p = [[Person alloc] init];
}
// 经过上一行代码后, 栈里面的变量a\b\c都会被回收
// 但是堆里面的Person对象还会留在内存中,因为它是计数器依然是1
return 0;
}
Two. Reference counters
1. To manage memory consumed by objects, a counter is introduced to manage memory
Common actions for referencing counters
To send an retain message to an object, you can make the reference counter value +1 (The return object of the Retain method itself)
To send a release message to an object, you can reference the counter-1
The current reference counter value can be obtained by sending an Retaincount message to the object.
It is important to note that release does not represent destroying/reclaiming objects, only counters-1
2. When the reference counter of an object is 0 o'clock, then the object is destroyed, the memory occupied by the system is reclaimed, but when the object is destroyed, the system will automatically send an DEALLOC message to the object, so we can determine whether the object is destroyed according to the Dealloc method has not been called
- (void)dealloc
{
[super dealloc]; // 注:重写dealloc方法时,[super dealloc]一定要写在大括号中最下面
}
3. Zombie object/Wild pointer/null pointer
1> Zombie Objects: Objects that have been destroyed (can no longer be used)
2>: Pointer to a Zombie object (memory address not available), sending a message to the wild pointer will report exc_bad_access error
3> NULL pointer: There is no pointer to the storage space (nil = = 0), and sending a message to the null pointer has no reaction
Three. Manual memory Management (MARC)
1. If you turn off automatic memory management arc
By default, Xcode does not control zombie objects, and using a piece of freed memory does not give an error. For easy commissioning, zombie object monitoring should be turned on
2. Memory Management Principles
Apple's official policy on memory management
1> who created who release:
If you create an object through Alloc,new,copy, or mutablecopy , you must call release or Autorelease
2> who retain who release:
Once you call retain, you must call the release
A summary is
Plus, there's a minus.
once let the object counter +1, it must be at the end to let the object counter -1
Memory Management One:
int main()
{
Person *p = [[Person alloc] init];
Student *stu = [[Student alloc] init];
[stu release];
[p release];
}
Memory Management II:
// 一个对象中拥有其他对象@property未用retain修饰时,在其对象销毁时,需要在系统调用dealloc方法时手动给其他对象release
// Person.h文件
#import <Foundation/Foundation.h>
@class Car;
@interface Person : NSObject
// 人拥有车
@property Car *car;
@end
// Person.m文件
#import "Person.h"
#import "Car.h"
@implementation Person
- (void)setCar:(Car *)car
{
if (_car != car) {
[_car release];
_car = [car retain];
}
}
- (void)dealloc
{
self.car = nil; // 当Person被销毁时,将其所拥有的Car对象release
[super dealloc];
}
@end
From for notes (Wiz)
1.6 Memory Management