2014-01-30 151 views
0

我嘗試在dispatch_async塊中使用autoreleasepool,但是it doesn't release the str。當timerEvent被重複調用時,會導致內存耗盡問題。爲什麼@autoreleasepool不起作用

- (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。現在,它的工作。

回答

1

您正在使用調度隊列,儘管調度隊列管理自動釋放池,但不能保證它們被清空的時間/點。這意味着你的對象將在一段時間後釋放。

我認爲你的計時器過於頻繁,因此你的無限內存增長(意味着你的對象沒有機會釋放並且你有內存警告)。

這裏是蘋果documentation。這裏有一個關於stackoverflow問題的link,它有一些很好的答案,請閱讀它們。