Management Category: OC objects
The memory resources that each computer system can use for the program are limited. There are two things we should care about:
Allocate when needed, release after use
Do not use any memory resources that have been freed, otherwise useless stale values cause a variety of errors to occur
Three kinds of management methods:
Manual admin Mode MRC (Manual referencing count)
Semi-automatic management mode Autoreleasepool
Automatic management mode ARC (auto referencing count)
Cocoa employs a technique called reference counting, which associates an integer for each object:
Related methods:
-(instancetype) retain // reference count +1
-(void) release // reference count -1
-(NSUInteger) retainCount // The current reference count value of the object
Use reference count:
A. When an object is created (alloc new copy), the reference count of the object being created is 1
B. The reference count is +1 (retain) when a piece of code accesses the object
C. When this code is complete, the reference count-1 (release)
D. When the reference count is 0 o'clock, indicating that no code is accessing the object, the object is destroyed
(An dealloc message is automatically sent to it before it is destroyed).
Scene interpretation: In-game rooms, replicas, etc.
Guidelines for Memory management:
1) Principles of memory management:
As long as someone else is using an object, the object will not be recycled;
As long as you want to use this object, then you should let the object reference counter +1;
When you do not want to use this object, you should let the object reference counter-1;
2) who created, who release
A. If you create an object through Alloc,new,copy, you must call the release or Autorelease method
B. If you don't create it, you don't have to be responsible.
3) who retain, who release
Summary: The beginning and ends, there are plus and minus.
1) As long as the alloc is called, there must be a release
2) The setter method of the attribute
A. Direct assignment of basic data types
-(void) Setage: (int) Age {_age = age;}
B. OC object, first judge and attribute old value is not the same object
If it is, do nothing; if not, release the old value and retain the new value
- (void) setCar:(Car *) car {
if ( car != _car ) {
[ _car release];
_car = [ car retain];
}
}
3) Dealloc method
A. Release once for properties held by self
b. [Super Dealloc] put in the last
- (void) dealloc {
[car release];
[super dealloc];
}
4) do not show the following junk code
Stu.car = [[Car alloc] init]; Causes the reference count to be 2[[car alloc] init].speed = 100; I can't release it.
@property should have only one memory management parameter.
Under MRC, memory management parameters include: Assign retain copy, default is assign
These memory management parameters can determine the implementation of the property setter method
Assign:setter does not modify the reference count
@property (nonatomic,assign) Nsinteger age;
Setter Method:
-(void) Setage: (Nsinteger) Age {_age = age;}
Retain: Modify the reference count for this property (as per the Code specification)
@property (nonatomic, retain) nsnumber * age;
Setter Method:
- (void)setAge:(NSNumber *)age {
if ( _age != age ) {
[_age release];
_age = [age retain];
}
}
Copy: Deep copy assignment, generally used for nsstring
@property (nonatomic, copy) NSString * name;
Setter Method:
- (void)setName:(NSString *)name {
if ( _name != nil ) {
[_name release];
}
_name = [name copy];
}
Circular reference problems with object composite relationships
Problem Description:
A person object, a card object, each property
If one memory parameter is retain, then the reference count should be 2
Do this for any object release, no object is destroyed
Workaround:
One end uses retain and one end uses assign
This article is from the "Teacheran" blog, make sure to keep this source http://annmeng.blog.51cto.com/3321237/1745688
OBJECTIVE-C (7) memory management MRC