Note that there are some explanatory texts on larger screens.

plurals
  1. PONSURLConnection messes up iPad memory
    text
    copied!<p>we build an iPad app that downloads a bunch of data and PDF documents from a web service (data first, documents later in the background). To do so, we use SOAP via HTTP(S) requests. It works fine and altogether, the app is running well. Problem is, if there are too many documents to download at some point the app crashes. Using Instruments I figured out that it is a memory issue, particularly NSRegularExpression and NSRunLoop. (I'm using ARC)</p> <p>I could improve my code to optimize the NSRegularExpression creation. But I don't know how to improve the NSRunLoop issue.</p> <p>I tried both, asynchronous and synchronous HTTP request. Using async, I had to wait for the download to finish and since sleep()/[NSThread sleepForTimeInterval:] aren't an option, I use</p> <pre><code>while ( _waitToFinish ) { [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; } </code></pre> <p>Using sync request, Instruments reveals that </p> <pre><code>[NSURLConnection sendSynchronousRequest:theRequest returningResponse:&amp;urlResponse error:&amp;error]; </code></pre> <p>also "waits" with help of NSRunLoop and also messes up the memory.</p> <p>Is this a bug in CoreFoundation or ARC?</p> <p>Is there another way to idle while waiting for the requests to finish?</p> <p>Thanks in advance.</p> <p><strong>Edit:</strong></p> <p>With "memory issue" I meant that the app crashes (or is killed by iOS) because it uses too much memory.</p> <p>This is what Instruments shows: <img src="https://i.stack.imgur.com/oGTeR.png" alt="Instruments screenshot"> The percentage get higher the longer the app is downloading.</p> <p><strong>Edit:</strong></p> <p>Going further down revealed that it is NSURLConnection, that is messing up the memory. It seems that I have somehow missed setting <code>connection</code> and <code>receivedData</code> to <code>nil</code> (see <a href="https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html" rel="nofollow noreferrer">URL Loading System Programming Guide</a>). This improved my memory usage again a little.</p> <p>Now, there are two more big memory allocation operations: <img src="https://i.stack.imgur.com/U6XWh.png" alt="enter image description here"></p> <p>And this is the code I think belongs to what Instruments displays:</p> <pre><code>-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [_receivedData appendData:data]; } -(void)connectionDidFinishLoading:(NSURLConnection *)connection { NSString *responseText = [[NSString alloc] initWithBytes:[_receivedData mutableBytes] length:[_receivedData length] encoding:NSUTF8StringEncoding]; self.lastResponse = responseText; responseText = nil; connection = nil; _receivedData = nil; _lastResult = TRUE; _waitToFinish = FALSE; } </code></pre> <p>Is there anything I could change to improve the code?</p> <p><strong>Edit:</strong> (Changed title from "NSRunLoop messes up iPad memory")</p> <p><strong>Edit:</strong> I created a test app to prove that it is the NSURLConnection, that messes up the memory. Then I contacted the Apple Developer Support.</p> <p>Since I am downloading a lot of PDF in an iteration with NSURLConnection, the solution was to add an <code>@autoreleasepool { .. }</code> in the iteration and another one around the <code>NSRunLoop</code>.</p> <p>Thanks.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload