Ios 提交到GCD串行队列的块会导致内存无限增长
我有一段代码,它在计时器回调中将块分派到串行队列。对于测试,块为空意味着其中没有任何代码。然而,我看到我的应用程序的内存分配在增加。是因为gcd没有足够快地释放提交的块吗?或者这与gcd正在生成的线程数量有关(如所示)? 我还尝试将串行队列更改为并发队列,并确实解决了内存增长问题,但我想知道除了使用并发队列之外,是否有人对如何解决该问题有很好的解释和建议 更新 下面是我用来调试此问题的示例代码:Ios 提交到GCD串行队列的块会导致内存无限增长,ios,afnetworking,grand-central-dispatch,dispatch-async,Ios,Afnetworking,Grand Central Dispatch,Dispatch Async,我有一段代码,它在计时器回调中将块分派到串行队列。对于测试,块为空意味着其中没有任何代码。然而,我看到我的应用程序的内存分配在增加。是因为gcd没有足够快地释放提交的块吗?或者这与gcd正在生成的线程数量有关(如所示)? 我还尝试将串行队列更改为并发队列,并确实解决了内存增长问题,但我想知道除了使用并发队列之外,是否有人对如何解决该问题有很好的解释和建议 更新 下面是我用来调试此问题的示例代码: - (BOOL)application:(UIApplication *)application d
- (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];
}
-(BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项{
self.httpRequestOperationManager=[[AFHTTPRequestOperationManager alloc]init];
self.httpRequestOperationManager.operationQueue.maxConcurrentOperationCount=1;
self.queue=dispatch\u queue\u create(“我的队列”,dispatch\u queue\u SERIAL);
NSString*urlString=@;
NSURL*url=[NSURL URLWithString:urlString];
self.request=[[NSURLRequest alloc]initWithURL:url缓存策略:NSURLRequestReloadIgnoringCacheData timeoutInterval:5];
[NSTimer scheduledTimerWithTimeInterval:0.5f
目标:自我
选择器:@selector(获取)
用户信息:无
重复:是];
返回YES;
}
-(作废)取回
{
AFHTTPRequestOperation*op=[self.httprequestoperation管理器
HTTPRequestOperationWithRequest:self.request
成功:^(AFHTTPRequestOperation*操作,id响应对象){
调度异步(self.queue^{
NSLog(@“异步调度导致内存增长”);
});
}失败:^(AFHTTPRequestOperation*操作,NSError*错误){
}];
[self.httpRequestOperationManager.operationQueue addOperation:op];
}
注释
有些代码在这里很有用。很有可能通过捕获强引用来保留循环,但是如果不看到代码,就很难知道。您提交的块是什么?内存可能正在自动释放池中积累。如果在块中使用了大量内存,可以使用它自己的@autoreleasepool{}块提交。然而,如果没有更多的背景,我们真的无法帮助你。谢谢你们的回复。我添加了一些示例代码(参见更新)。我认为这与在串行队列上使用AFNetworking和异步调度有关,原因我不确定。希望有人能遮住一些光线!