gpt4 book ai didi

ios - 使用 AFNetworking 下载时泄漏

转载 作者:行者123 更新时间:2023-11-28 21:53:43 26 4
gpt4 key购买 nike

基本上当文件下载完成后,内存中就会有一些永远不会被释放的“东西”。这是导致该问题的代码的一个简单示例,内存上升到大约 50mb,它只是坐在那里,永远不会被释放(见下面的屏幕截图)。知道发生了什么事吗?

-(void)download {
NSString* urlString = @"http://download.thinkbroadband.com/50MB.zip";
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
AFHTTPRequestOperation* operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
NSLog(@"%lldmb of %lldmb downloaded", totalBytesRead / 1024 / 1024, totalBytesExpectedToRead / 1024 / 1024);
}];

[operation setCompletionBlockWithSuccess:^(__weak AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Download completed.");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error.localizedDescription);
}];

[operation start];
}

下载前:

enter image description here

下载后:

enter image description here

最佳答案

在我自己使用 AFNetworking 2.5 在 iOS 8.1 上进行的测试中,这似乎是 Debug模式而非 Release模式的情况。鉴于您的代码没有按原样运行,我做了以下测试用例:

- (IBAction)startDownload:(UIButton *)sender 
{
NSString *downloadURLString = @"http://mirror.internode.on.net/pub/test/50meg.test";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:downloadURLString]];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
AFHTTPRequestOperation *operation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Download succeeded.");
self.stateLabel.text = @"Download succeeded.";
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Download failed with error: %@", error);
self.stateLabel.text = @"Download failed.";
}];

[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
NSLog(@"Bytes read: %lu", bytesRead);
NSLog(@"Total bytes read %lld", totalBytesRead);
NSLog(@"Total progress: %Lf", (long double)totalBytesRead / totalBytesExpectedToRead);
self.progressView.progress = (long double)totalBytesRead / totalBytesExpectedToRead;
}];

NSLog(@"Starting download.");
NSOperationQueue *downloadQueue = [[NSOperationQueue alloc] init];
[downloadQueue addOperation:operation];
}

使用这段代码,一旦下载完成,在 Release模式下,内存使用率会回落到下载前的水平。

关于ios - 使用 AFNetworking 下载时泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27326846/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com