3

On iOS, our application is downloading a zip file that's approximately 400MB. We're getting intermittent crashing while the file is downloading.

Current I'm using [NSFileHandle writeData:] to write the data as it comes in, and is not being stored in memory. But I'm wondering if the operating system is somehow storing it in memory?

Would NSOutputStream be a better solution to downloading large file? Or possibly standard unix file descriptors?

Our file handle is setup like this:

NSFileManager * fileManager = [NSFileManager defaultManager];
[fileManager createFileAtPath:tmpFilePath.path contents:nil attributes:nil]; 
_zipFile = [NSFileHandle fileHandleForWritingAtPath:tmpFilePath.path];

Currently my NSURLConnection delegate method looks like this:

- (void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data {
   [_zipFile writeData:data];
}

So the data that comes in from the request is not stored or appended to any other data objects. Shouldn't this just write to disk and not effect memory?

Thanks

gngrwzrd
  • 5,902
  • 4
  • 43
  • 56
  • I use `NSOutputStream` for precisely this purpose and it works fine. (It's the pattern Apple demonstrated in [SimpleURLConnections sample](http://developer.apple.com/library/ios/#samplecode/SimpleURLConnections/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009245).) But I just tried it both ways, and both appear to be quite respectful in terms of the memory consumed (40mb file, memory spiked to 3mb during download but quickly leveled off at 1mb). I suspect the problem must rest elsewhere (zombies or something else). How did you conclude that the `NSFileHandle` was the source of the problem? – Rob Jun 28 '13 at 02:22
  • Are you using zombies? What does allocations tool report? – Rob Jun 28 '13 at 02:25
  • This download is the only thing happening in the application. The rest of the app depends on this zip file being downloaded so there's one view controller with a progress bar for this download. Which tool in Instruments would you recommend I use to monitor? – gngrwzrd Jun 28 '13 at 17:49
  • Also, run your code through the static analyzer ("Analyze" on the "Product" menu), to make sure you don't have any issues. (You should have a clean bill of health there; if there are any issues reported, you must fix those.) Finally, you might run your code through the "Leaks" tool, to make sure it's not reporting any leaks (though I can't imagine any in the code you've provided thus far). – Rob Jun 28 '13 at 17:58

1 Answers1

4

I don't think there's anything wrong with your use of NSFileHandle. I confess that I've always used NSOutputStream, but I just tried it both ways (NSFileHandle and NSOutputStream), and both appear to be quite respectful in terms of the memory consumed. Downloading a 40mb file, the allocations spiked to 3mb at the start of the download but quickly leveled off at 1mb):

allocations and leaks

So, I'd run your app through "Allocations" and "Leaks" (if you choose "Leaks", you get both) and see what it looks like.

If you haven't already, run your code through the static analyzer ("Analyze" on the "Product" menu), to make sure you don't have any issues. (You should have a clean bill of health there; if there are any issues reported, you must fix those.) I'd also make sure that zombies are turned off (because in the process of keeping track of all of those released objects, it creates a zombie object for each ... small, but it will consume memory.

Rob
  • 415,655
  • 72
  • 787
  • 1,044