Reputation: 25959
Basically when the file is done downloading, there's "something" in memory that never gets released. Here's a simplistic example of the code causing that problem, memory goes up to around 50mb and it just sits there and never gets released (see screenshots below). Any idea of what's going on?
-(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];
}
Before download:
After download:
Upvotes: 0
Views: 284
Reputation: 12770
In my own testing on iOS 8.1 using AFNetworking 2.5, this appears to be the case in debug but not release mode. Given that your code didn't work as is, I made the following test case:
- (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];
}
With this code, once the download is complete, in release mode, the memory usage drops back to the pre download level.
Upvotes: 2