我有一段代码,在定时器回调上将块调度到一个串行队列。对于测试,块是空的意味着它没有任何代码。不过,我看到我的应用程序的内存分配在增加。是因为gcd没有足够快地释放提交的块吗?或者它与gcd产生的线程数有关(如Memory usage for global GCD queue所示)? 我也尝试用并发队列更改串行队列,并确实解决了内存增长问题,但我想知道是否有人对如何解决问题有一个很好的解释和建议,而不是使用并发队列。
更新
以下是我用来调试此问题的示例代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.httpRequestOperationManager = [[AFHTTPRequestOperationManager alloc] init];
self.httpRequestOperationManager.operationQueue.maxConcurrentOperationCount = 1;
self.queue = dispatch_queue_create("my_queue", DISPATCH_QUEUE_SERIAL);
NSString *urlString = @"<URL>";
NSURL *url = [NSURL URLWithString:urlString];
self.request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:5];
[NSTimer scheduledTimerWithTimeInterval:0.5f
target:self
selector:@selector(fetch)
userInfo:nil
repeats:YES];
return YES;
}
- (void)fetch
{
AFHTTPRequestOperation *op = [self.httpRequestOperationManager
HTTPRequestOperationWithRequest:self.request
success:^(AFHTTPRequestOperation *operation, id responseObject) {
dispatch_async(self.queue, ^{
NSLog(@"DISPATCHING ASYNC CAUSES MEMORY TO GROWTH");
});
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
[self.httpRequestOperationManager.operationQueue addOperation:op];
}
备注
下图显示了12分钟后的内存增长情况。内存分配继续增长!