简体   繁体   English

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

[英]Memory usage increases if block is run in a queue

I create a queue 我创建一个队列

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

and put my blocks in it in a timer event, my memory usage keeps going up: 并在计时器事件中将我的块放入其中,我的内存使用量不断上升:

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

Without the queue, it works fine: 没有队列,它可以正常工作:

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

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

}
}

I am using ARC. 我正在使用ARC。

Looks like the img object is not getting released if its in the queue. 如果img对象不在队列中,则该对象似乎未释放。

How can I fix this mess? 我该如何解决此问题?

There is no defined autorelease pool when running in a queue. 在队列中运行时,没有定义的自动释放池。 There is when a timer fires. 有一个计时器触发。

Thus, change your code to this: 因此,将代码更改为此:

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

UIImage should not cause issues when used from the non-main thread and the documentation explicitly states this. UIImage非主线程使用UIImage不会引起问题,并且文档中明确指出了这一点。 If that still shows memory accretion, I'd suggest you use the Allocations instrument to find out what and why. 如果那仍然显示内存增加,建议您使用“分配”工具找出原因和原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM