如果在队列中运行块,则内存使用量会增加

时间:2016-01-19 15:09:20

标签: objective-c memory-management objective-c-blocks

我创建了一个队列

dispatch_queue_t serialq = dispatch_queue_create("com.osletek.hill-billy", DISPATCH_QUEUE_SERIAL);

并在计时器事件中将我的块放入其中,我的内存使用率持续上升:

-(void) onTimer
{
 __weak typeof(self) this = self;
 dispatch_async(serialq, ^{
  UIImage *img = [this screenShot];
  // do something with img ...
 });
}
}

没有队列,它可以正常工作:

-(void) onTimer
{
 __weak typeof(self) this = self;

  UIImage *img = [this screenShot];
  // do something with img ...

}
}

我正在使用ARC。

如果img对象在队列中,它看起来不会被释放。

我该如何解决这个烂摊子?

1 个答案:

答案 0 :(得分:1)

在队列中运行时没有定义的自动释放池。有一个计时器启动。

因此,将代码更改为:

__weak typeof(self) this = self;
   dispatch_async(serialq, ^{
      @autorelease {
          UIImage *img = [this screenShot];
          // do something with img ...
      }
    });
}
从非主线程使用时,

UIImage不应该导致问题,文档明确说明了这一点。如果仍然显示内存增加,我建议你使用Allocations工具找出原因和原因。