1

I try to use autoreleasepool in a dispatch_async block, but it doesn't release the str. When timerEvent is repetitively called, it will lead to a run out of memory problem.

- (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);
         }
    });
} 

Thank you for your help.

----- Solved --------------- Thanks to C_X

The timer interval has been set too small. In my case, I find it should be at least 0.004. Now, it works.

1 Answers1

2

You are using dispatch queues although dispatch queues do manage autorelease pools, no guarantee is made regarding the time/point they are emptied. It means your object will release but some time later.

I think your timer is too frequent, due to which your are going in unbounded memory growth (means your objects did't get chance to deallocate and you got memory warning).

Here is apple documentation. Here is a link of stackoverflow question which got some good answers about it please read them.

Community
  • 1
  • 1
Adnan Aftab
  • 14,377
  • 4
  • 45
  • 54