iOS开发中@autoreleasepool使用说明

Document

https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAutoreleasePools.html

About Autorelease Pool Blocks

  1. 代码块

    @autoreleasepool {
        // Code that creates autoreleased objects.
    }
    

    At the end of the autorelease pool block, objects that received an autorelease message within the block are sent a release message—an object receives a release message for each time it was sent an autorelease message within the block.

    在的autoreleasepool代码块执行结束的时候,自动释放池收到一条autorelease消息,池中的对象收到一条release的消息。

  2. autorelease的嵌套使用

    @autoreleasepool {
    // . . .
         @autoreleasepool {
            // . . .
            }
    . . .
    }
    

Using Autorelease Pool Blocks

There are, however, three occasions when you might use your own autorelease pool blocks:

  • If you are writing a program that is not based on a UI framework, such as a command-line tool.

  • If you write a loop that creates many temporary objects.

    You may use an autorelease pool block inside the loop to dispose of those objects before the next iteration. Using an autorelease pool block in the loop helps to reduce the maximum memory footprint of the application.

  • If you spawn a secondary thread.
    You must create your own autorelease pool block as soon as the thread begins executing; otherwise, your application will leak objects

什么时候使用@autpreleasepool

  1. 写基于命令行的的程序时,就是没有UI框架,如AppKit等Cocoa框架时。(使用的较少)

  2. 写循环,循环里面包含了大量临时创建的对象。(使用AudioQueue播放音频读取频繁数据的例子)

    static void IJKSDLAudioQueueOuptutCallback(void * inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer) {
        @autoreleasepool {
            IJKSDLAudioQueueController* aqController = (__bridge IJKSDLAudioQueueController *) inUserData;
    
            if (!aqController) {
                // do nothing;
            } else if (aqController->_isPaused || aqController->_isStopped) {
                memset(inBuffer->mAudioData, aqController.spec.silence, inBuffer->mAudioDataByteSize);
            } else {
                (*aqController.spec.callback)(aqController.spec.userdata, inBuffer->mAudioData, inBuffer->mAudioDataByteSize);
            }
    
            AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, NULL);
        }
    }
    

    写在@autpreleasepool 代码块中,让每次执行完成后可以及时的释放临时对象的内存

  3. 创建了新的线程。(非Cocoa程序创建线程时才需要)

    static void *SDL_RunThread(void *data)
    {
        @autoreleasepool {
            SDL_Thread *thread = data;
            pthread_setname_np(thread->name);
            thread->retval = thread->func(thread->data);
            return NULL;
        }
    }
    

参考文档

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容