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 usingallocnewcopyWhen 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 objectretainMessage, the reference to this object counts as +1
  • Send one to an objectreleaseMessage, the reference to this object counts the value -1
  • Send an objectretainCountMessage 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 generatesgettermethods
  • readwrite: is generatedgetterWill be generatedsetterMethod, what is not written by default is readwrite
  • getter: Can be generatedgetterGive the method a name
  • setter: Can be generatedsetterGive the method a name
  • retain: will automatically help us generatesetterMethod memory management code
  • assign: will not help us generatesetterMethod memory management code generates only plaingetter/setterMethod, 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 objectdeallocMessage (hence, fromdeallocMethod is not called to determine whether the object was destroyed.
  • deallocMethods to rewrite
    • General rewritingdeallocMethod, where the associated resources are freed (remove listeners, remove coreFoundation objects, and so on)
    • Under MRC, once rewrittendeallocMethod must be called[super dealloc]And put it in the last call
  • Use attention
    • Indirect calldealloc
    • Don’t indeallocMethod to call other methods
    • Once an object is reclaimed, its memory is no longer available

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)