zoukankan      html  css  js  c++  java
  • Autorelease pool

    根据苹果官方文档中对 Using Autorelease Pool Blocks 的描述,我们知道在下面三种情况下是需要我们手动添加 autoreleasepool 的:

    1. 如果你编写的程序不是基于 UI 框架的,比如说命令行工具;

    2. 如果你编写的循环中创建了大量的临时对象;

    3. 如果你创建了一个辅助线程。

    Cocoa always expects code to be executed within an autorelease pool block, otherwise autoreleased objects do not get released and your application leaks memory. (If you send an autorelease message outside of an autorelease pool block, Cocoa logs a suitable error message.) The AppKit and UIKit frameworks process each event-loop iteration (such as a mouse down event or a tap) within an autorelease pool block. Therefore you typically do not have to create an autorelease pool block yourself, or even see the code that is used to create one. 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. (See Autorelease Pool Blocks and Threads for details.)

  • 相关阅读:
    spring1冲刺感想与总结
    Scrum 项目7.0
    读《构造之法》8、9、10章有感
    Scrum4.0+5.0
    scrum3.0
    0512操作系统之进程调度
    Scrum 项目1.0
    团队之阅读感想
    sprint3终极演示
    0622—软件工程第一学期总结
  • 原文地址:https://www.cnblogs.com/studyNT/p/5296848.html
Copyright © 2011-2022 走看看