This is the 14th day of my participation in the August More Text Challenge. For details, see: August More Text Challenge

Autoreleasepool

Autoreleasepool is used to hold objects that need to be released at a later point. When the Autoreleasepool is cleared, the system sends release messages to the objects in the Autoreleasepool

Curly braces define the range of the pool to be automatically released. Open curly braces are created, close curly braces are automatically released, and at the end of the range, the objects in brackets are reclaimed`release`"@ autoreleasepool {}Copy the code

Note: Only one release is sent, and if the reference count is not zero at the time, the object is still not released

  • AutoreleasepoolMethods return the object itself (MRC)
    Penson *p = [Person new]; P = [p autorelease];Copy the code
  • Call outAutoreleasepoolMethod, the object’s counter remains unchanged (MRC)
    Person *p = [Person new];
    p = [p autorelease];
    NSLog(@"count= %d",[p retainCount]);/ / 1
    Copy the code

The principle of Autoreleasepool

For each Autoreleasepool, the system simply puts the Object in the current Autoreleasepool. When the pool is released, All objects in the pool will be called release


The advantages of Autoreleasepool

  • Don’t worry about object release time
  • You don’t care when it’s called, rightrelease
// Create an automatic release pool
@autoreleasepool{    

   Person *p = [[Person alloc]init];
   // We can use p wherever we have access to it
   // We don't need to call release as long as we call autoRelease
   p = [p autorelease];
   
}
// The automatic release pool is destroyed. Send a release message to all objects in the automatic release pool
Copy the code

Precautions for Autoreleasepool

  • You must call Autoreleasepool in the Autoreleasepool to place an object in the MRC.

  • When an object is created in the automatic releasepool, it must be called Autoreleasepool before the object is placed in the automatic releasepool (MRC).

    @autoreleasepool{
    Person *p =[[[Person alloc]init] autorelease];
    }
    Copy the code
  • Do not use memory-consuming objects in the automatic free pool

    @autoreleasepool{
        Person *p =[[[Person alloc]init] autorelease];
        / / n lines of code
    }
    Copy the code
  • Try not to automatically release the pool to use the cycle, especially the number of cycles

    @autoreleasepool{
        for(int I =0; i <99999; I + +) {// Each call creates a new object
            // Each object occupies one block of storagePerson *p =[[[Person alloc]init] autorelease]; }}// Objects created in the loop will remain in the pool until this point
    Copy the code
  • N automatic release pools can be created in a program, and automatic release pools can be nested, if there are multiple automatic release pools, then automatic release pools will be stored in the form of “stack”, in the first and the last

    @autoreleasepool{
        // Create the first automatic release pool
        @autoreleasepool{
            // Create a second automatic release pool
            @autoreleasepool{
                // Create a third automatic release pool
            }
            // Destroy the first automatic release pool
        }
        // Destroy the second automatic release pool
    }
    // Destroy the third automatic release pool
    Copy the code

Use Autoreleasepool to reduce peak memory (ARC)

By wrapping the code inside the loop in the Autoreleasepool, objects that are automatically released in the loop will be placed in this pool, thus reducing the peak memory (peak memory: the maximum amount of memory used by the app in a given period of time).

for(int I =0; i <99999; I ++) {@autoreleasepool{Person *p =[[Person alloc]init]; [array addObject:p]; }}Copy the code

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)