NSURLConnection: "Greedy Memory Eater" and Error 303
NSURLConnection: "Greedy Memory Eater" and Error 303
- Subject: NSURLConnection: "Greedy Memory Eater" and Error 303
- From: Andreas Grosam <email@hidden>
- Date: Fri, 09 Sep 2011 13:06:23 +0200
Dir Members,
I'm trying to use a NSURLConnection to download a large data file (>10 MB) from a web service to the iPhone. Currently, testing on iOS 4.2.1, on a device. The connection is established over WIFI.
The problem I get with NSURLConnection is, that it internally uses a lot (really a lot) memory for its data buffers. It seems, the connection reads as much data as it can from the network and a tries to safe it in internal buffers, no matter what, apparently until the system cannot provide more memory.
In a simple test app, NSURLConnections quite quickly consumes up to 10Mbyte for a number of its buffers - on the iPhone!, which is probably the maximum before the app will be killed. With Instruments, it can be viewed quite easily.
The download works as long as the processing of each received buffer (NSData object) does not require further memory. Otherwise, since almost all available memory is allocated by the internals of NSURLConnection, the app will crash quite early.
Another problem is, if I process the incoming data buffers more "slowly", then it may occur that NSURLConnection returns the following error:
Error Domain=kCFErrorDomainCFNetwork Code=303 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error 303.)" UserInfo=0x3911830 {NSErrorFailingURLKey=http://ag-macbookpro.local:3000/download/download, NSErrorFailingURLStringKey=http://ag-macbookpro.local:3000/download/download}
The slower I process the data, the bigger the internal buffers become, and the sooner I get this error. It does also not make any difference if the thread where the data will be received will be blocked (this slows down the insertion of data objects into a FIFO, but anyway, NSURLConnection will consume the max available memory for its internal buffers).
The NSURLConnection is setup using the asynchronous method. I tried two approaches, first scheduling it on the main thread, and second scheduling it on a secondary thread. There is basically no difference.
In the test, the processing of the data buffers will not require additional memory, and due to this downloading works in this scenario - unless the speed to process the received data buffers drops too a certain limit. However, my real app would need to allocate additional objects when processing the data. Then, there is a 100% guarantee that the app will crash. So, currently, I have no idea how to make this work (using a NSURLConnection) unless I can tell NSURLConnection to be less greedy, and throttle the reading of data from the network somehow. And to be honest, this behavior seems to be a bug at least when using it on the iPhone.
Any ideas?
Thanks in advance for tips!
Regards
Andreas_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden