简体   繁体   中英

Memory pressure while downloading a large size file using GoldRaccoon(ftp library)

I use GoldRaccoon as my 3rd ftp library. When I download large size (> 500MB) file, my app will crash in memory pressure.

The snapshot as below that using instrument to detect the memory allocations in my app.

在此处输入图片说明

在此处输入图片说明

It seems self.receivedData takes too much memory allocations. How do I handle these allocation when getting low memory warning ??

I know this is old but I was just dealing with the exact same thing and I'm sure someone else is too. It's very easy to fix, not sure why it wasn't implemented like this in the first place.

In GRRequestsManager.m, replace this method with this.

- (void)dataAvailable:(NSData *)data forRequest:(id<GRDataExchangeRequestProtocol>)request
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:[request localFilePath]])
    {
        [data writeToFile:[request localFilePath] atomically:YES];
    }
    else
    {
        NSFileHandle *handle = [NSFileHandle fileHandleForUpdatingAtPath:[request localFilePath]];
        [handle seekToEndOfFile];
        [handle writeData:data];
        [handle closeFile];
    }
}

Then in the - (void)requestCompleted:(GRRequest *)request method, remove the writing to file in the download section. That's it, now instead of storing the file up in NSData and writing at the end, it just keeps adding the data to the file as it goes. I didn't take the time to make it pretty or handle write errors, but it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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