2014-12-06 87 views
0

基本上,当文件下载完成时,内存中的某些内容永远无法释放。下面是导致这个问题的代码的一个简单例子,内存高达50mb左右,它只是坐在那里,永远不会被释放(见下面的截图)。对发生什么事有任何想法?使用AFNetworking下载时出现泄漏

-(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

回答

2

在iOS上使用AFNetworking 2.5 8.1我自己的测试,这似乎是在调试的情况下但不是释放模式。由于你的代码没有作为是工作,我做了如下测试案例:

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

有了这个代码,一旦下载完成后,在释放模式,内存使用量回落到前期水平下载。

+0

谢谢!我会尽快给这个镜头=)。 – Carlo 2014-12-06 19:03:50

+0

有趣的是,它并没有为我工作。内存开始时约11mb,下载完成后5分钟,它仍然在42.8mb ...我也使用AFNetworking 2.5.0和iOS 8.1。 – Carlo 2014-12-06 19:09:14

+0

您确定您处于发布模式?我通过仪器进行了测试,默认情况下它们以发布模式构建。 – 2014-12-08 03:41:32