【问题标题】:Why @autoreleasepool doesn't work为什么@autoreleasepool 不起作用
【发布时间】:2014-01-30 10:40:26
【问题描述】:

我尝试在dispatch_async 块中使用autoreleasepool,但it doesn't release the str。当timerEvent被重复调用时,会导致内存不足的问题。

- (void)viewDidLoad
{
    [super viewDidLoad];
    [NSTimer scheduledTimerWithTimeInterval:0.0001 target:self selector:@selector(timerEvent) userInfo:nil repeats:YES];

}
-(void)timerEvent
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
         @autoreleasepool {
             NSString *str =[NSString stringWithFormat:@"%d and %d",px,py];
             NSLog(str);
         }
    });
} 

感谢您的帮助。

----- 解决了 ---------------- 感谢 C_X

计时器间隔设置得太小。就我而言,我发现它至少应该是 0.004。现在,它可以工作了。

【问题讨论】:

    标签: ios memory timer grand-central-dispatch autorelease


    【解决方案1】:

    尽管调度队列确实管理自动释放池,但您正在使用调度队列,但不能保证它们被清空的时间/点。这意味着您的对象将在一段时间后释放。

    我认为您的计时器过于频繁,因此您的内存会无限增长(意味着您的对象没有机会释放并且您收到内存警告)。

    这是苹果documentation。这是一个 link 的 stackoverflow 问题,它得到了一些很好的答案,请阅读它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-01
      • 2015-09-07
      • 2013-03-07
      相关资源
      最近更新 更多