NSURLConnection messes up iPad memory

北城余情 提交于 2019-12-11 03:27:11

问题


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)

I could improve my code to optimize the NSRegularExpression creation. But I don't know how to improve the NSRunLoop issue.

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

while ( _waitToFinish ) {
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}

Using sync request, Instruments reveals that

[NSURLConnection sendSynchronousRequest:theRequest returningResponse:&urlResponse error:&error];

also "waits" with help of NSRunLoop and also messes up the memory.

Is this a bug in CoreFoundation or ARC?

Is there another way to idle while waiting for the requests to finish?

Thanks in advance.

Edit:

With "memory issue" I meant that the app crashes (or is killed by iOS) because it uses too much memory.

This is what Instruments shows:

The percentage get higher the longer the app is downloading.

Edit:

Going further down revealed that it is NSURLConnection, that is messing up the memory. It seems that I have somehow missed setting connection and receivedData to nil (see URL Loading System Programming Guide). This improved my memory usage again a little.

Now, there are two more big memory allocation operations:

And this is the code I think belongs to what Instruments displays:

-(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;
}

Is there anything I could change to improve the code?

Edit: (Changed title from "NSRunLoop messes up iPad memory")

Edit: I created a test app to prove that it is the NSURLConnection, that messes up the memory. Then I contacted the Apple Developer Support.

Since I am downloading a lot of PDF in an iteration with NSURLConnection, the solution was to add an @autoreleasepool { .. } in the iteration and another one around the NSRunLoop.

Thanks.


回答1:


It's not a bug in Core Foundation or ARC. It's a bug in your code.

Don't run the run loop yourself. Just use +[NSURLConnection sendAsynchronousRequest:queue:completionHandler:]. It will call your completionHandler block when the request is complete. That's when you process the response. Meanwhile, just return out of your method and let the system worry about running the run loop.

You say “it's a memory issue” with NSRegularExpression and NSRunLoop, but you don't say what the issue is, or what evidence Instruments is showing you. If you describe the “issue” in more detail, maybe we can help you with that.



来源:https://stackoverflow.com/questions/9512362/nsurlconnection-messes-up-ipad-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!