iOS自动释放池块 [英] iOS autorelease pool blocks

查看:233
本文介绍了iOS自动释放池块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我到 autorelease pool blocks 时,我正在阅读关于内存管理的苹果文档。有些东西让我思考。

I was reading the documentation from apple about memory management when I got to autorelease pool blocks and something got me thinking.

 Any object sent an autorelease message inside the autorelease pool block is  
 released at the end of the block.

我不确定我是否完全理解这一点。在自动释放池块中创建的任何对象无论如何都会在块的末尾释放,因为这是它的生命周期。当它到达块的末尾时,为什么要在对象被释放时调用autorelease?

I am not sure I fully understand this. Any object created inside an autorelease pool block gets released at the end of the block anyway because that is it's life span. Why would you need to call autorelease to the object when it is going to get released anyway when it reaches the end of the block?

为了更清楚,我将给出一个例如,我在想什么:

To be clearer, I will give an example, of what I am thinking:

   @autoreleasepool {

    MyObject *obj = [[MyObject alloc] init]; // no autorelease call here

    /* use the object*/
   //....
   // in the end it should get deallocated because it's lifespan ends, right?
   // so why do we need to call autorelease then?!
  }

PS:请不要告诉我因为ARC我们不需要做一些事情,因为ARC会照顾它们。我完全清楚这一点,但我想暂时将ARC放在一边,以了解内存管理的机制。

PS: Please don't tell me that because of ARC we don't need to do some things because ARC takes care of them. I am fully aware of that, but I want to leave ARC aside for just a few moments to understand the mechanism of memory management.

推荐答案

Autorelease只是从对象中删除一个保留计数,它不像c中那样立即释放内存。当自动释放池结束时,所有自动释放的对象计数为0将释放其内存。

Autorelease just removes a retain count from the object it does not "free" the memory immediately like in c. When the autorelease pool ends all auto released objects with a count of 0 will have their memory freed up.

有时您会创建大量对象。一个例子是每次迭代时创建新字符串并将新数据添加到字符串的循环。您可能不需要以前版本的字符串,并且想要释放那些使用的内存。您可以通过显式使用自动释放池来完成此操作,而不是等待它自然完成。

Sometimes you create a lot of objects. An example would be a loop that is creating new strings every time it iterates and adds new data to the string. You may not need the previous versions of the string and will want to free up memory used by those. You can accomplish this by explicitly using the autorelease pool instead of waiting for it to be done naturally.

//Note: answers are psudocode

//Non Arc Env
@autoreleasepool 
{

    MyObject *obj = [[MyObject alloc] init]; // no autorelease call here
    //Since MyObject is never released its a leak even when the pool exits

}
//Non Arc Env
@autoreleasepool 
{

    MyObject *obj = [[[MyObject alloc] init] autorelease]; 
    //Memory is freed once the block ends

}
// Arc Env
@autoreleasepool 
{

    MyObject *obj = [[MyObject alloc] init]; 
    //No need to do anything once the obj variable is out of scope there are no strong pointers so the memory will free

}

// Arc Env
MyObject *obj //strong pointer from elsewhere in scope
@autoreleasepool 
{

    obj = [[MyObject alloc] init]; 
    //Not freed still has a strong pointer 

}

这篇关于iOS自动释放池块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆