简体   繁体   English

AFNetworking下载多个文件内存占用

[英]AFNetworking downloading multiple files memory footprint

I'm using ARC and I'm have an issue downloading multiple files (10000+) in my app when using AFNetworking. 我正在使用ARC,在使用AFNetworking时,在我的应用程序中下载多个文件(10000+)时遇到问题。 I'm creating each AFHTTPRequest and then queuing them to be executed. 我正在创建每个AFHTTPRequest,然后将它们排队等待执行。 When I run this using instruments, I can see the memory usage increasing and increasing until the all the files have been downloaded, only then is all the memory released. 当我使用仪器运行时,我可以看到内存使用量的增加和增加,直到所有文件都被下载,然后才释放所有内存。 This survives on the simulator, but crashes when running on an iOS device. 这在模拟器上存活,但在iOS设备上运行时崩溃。

Below is a sample code of what i'm doing 下面是我正在做的示例代码

for (int i = 0; i < NUM_OF_FILES_TO_DOWNLOAD; i++) {
    _imageCounter++;
    [self downloadFile:i];
}

- (void)downloadFile:(NSUInteger)num
{    
    NSString *fileURL = @"file/%d/", num];
    NSURLRequest *url = [self.httpClient requestWithMethod:@"GET" path:fileURL parameters:nil];

    AFHTTPRequestOperation *requestOperation = [_httpClient HTTPRequestOperationWithRequest:url success:^(AFHTTPRequestOperation *operation, id responseObject) {
     ...
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
     ...
    }];

    [_httpClient enqueueHTTPRequestOperation:requestOperation];    
}

It appears that the files( responseObject ) aren't being released after the operation success block is executed and that is what is continually increasing the memory footprint? 执行操作成功块后,文件( responseObject )似乎没有被释放,这是不断增加内存占用量的原因? Is this correct? 这个对吗? Am I using the AFNetworking and the operationQueue as intended? 我是否按预期使用AFNetworking和operationQueue?

你可能想在你的for循环中放一个@autoreleasepool。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM