2014-12-06 52 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