This is the 12th day of my participation in the August More Text Challenge. For details, see: August More Text Challenge
Reference counter
The OC language uses reference counting to manage memory. Every object has a counter that can be incremented and decremented. If the object is referenced, the object’s reference count is incremented, and if not, the object’s reference count is decremented until it reaches zero, and the object is ready to be destroyed
What a reference counter does
- Represents the number of times an object has been referenced
- View reference count calls for an object
- (NSUInteger)retainCount
- When using
alloc
、new
、copy
When an object is created, the reference counter for the object defaults to 1 - The object is only reclaimed when no one is using it
- Memory used by an object is reclaimed only when its reference counter is 0
- If the reference count of an object is not zero, the memory used by the object cannot be reclaimed (unless the entire program exits).
How reference counters work
- Send one to an object
retain
Message, the reference to this object counts as +1 - Send one to an object
release
Message, the reference to this object counts the value -1 - Send an object
retainCount
Message can be obtained when there is an object reference count
Note: Release does not mean destroy or reclaim the object, just the counter -1
Memory management in attribute access methods (retain, copy, assign)
readonly
: only generatesgetter
methodsreadwrite
: is generatedgetter
Will be generatedsetter
Method, what is not written by default is readwritegetter
: Can be generatedgetter
Give the method a namesetter
: Can be generatedsetter
Give the method a nameretain
: will automatically help us generatesetter
Method memory management codeassign
: will not help us generatesetter
Method memory management code generates only plaingetter/setter
Method, the default is nothing, rightassign
nonatomic
: used in multithreading, low performance (default)atomic
: Used in multithreading, high performance
- (void)setName:(NSString *)name{
if(_name ! = name) {// Check whether the object passed in is new
[_name release]; // Release the object before _name
_name = [name retain]; // Assign the address of the name object to _name, then name and _name point to the same object}} - (void)setName:(NSString *)name{
if(_name ! = name) { [_name release];// Release the object before _name
_name = [name copy];// Copy the address of the name object to _name
}
}
-(void)setName:(NSString *)name{
_name = name; // Direct assignment
}
Copy the code
Dealloc method
- When an object’s reference counter is 0, the object is about to be freed and its memory is reclaimed
- When an object is about to be destroyed, the system automatically sends a message to the object
dealloc
Message (hence, fromdealloc
Method is not called to determine whether the object was destroyed. dealloc
Methods to rewrite- General rewriting
dealloc
Method, where the associated resources are freed (remove listeners, remove coreFoundation objects, and so on) - Under MRC, once rewritten
dealloc
Method must be called[super dealloc]
And put it in the last call
- General rewriting
- Use attention
- Indirect call
dealloc
- Don’t in
dealloc
Method to call other methods - Once an object is reclaimed, its memory is no longer available
- Indirect call
IOS Memory management
- Memory Management in iOS (Basic concepts)
- Memory Management in iOS (reference counters)
- Memory Management for iOS (ARC)
- Memory Management in iOS (Autoreleasepool)