Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSURLSession HTTP/2 memory leak

This My Test cases, point out that when using NSURLSession with a HTTP/2 connection there is memory problem.

test1: iOS 9. HTTP/2 server

I use NSURLSession to upload 10M file to a HTTP/2 server, if the file uploaded completed everything is ok, But if I cancel the upload task before it's completed, the 10M will never release.

test2: iOS 9. HTTPs1.1 server

I test the same code with a https1.1 file server, I cancel the upload task or not, everything is ok, the memory back to normal.(10M data is released)

test3 iOS 8. HTTP/2 server

This case everything is ok.(NSURLSession did not protocol negotiation to HTTP/2)

So, Even there is some thing not appropriate with my using NSURLSession, NSURLSession performance is not normal with HTTP/2.

Besides memory problem, when using NSURLSession with HTTP/2 to uploading file the progress segment size is huge(May 2M 'didSendBodyData' at one call back)

I also had read this page. SSL may cache some thing, but should not cache the whole file.(When I cancel the task or request timed out, 10M file size memory leaks)

Anyone Knows what cause the problem, could give me some help. Thanks.


Question update 0912: add a test project link

Test project :https://github.com/upyun/swift-sdk/tree/testleak

file:UPUtils.swift
//Change the url to make comparison test. 

//let DEFAULT_UPYUN_FORM_API_DOMAIN = "http://v0.api.upyun.com"//http1.1
//let DEFAULT_UPYUN_FORM_API_DOMAIN = "https://httpbin.org/post" //https1.1
let DEFAULT_UPYUN_FORM_API_DOMAIN = "https://v0.api.upyun.com"//http2
like image 717
rotoava Avatar asked Sep 09 '16 10:09

rotoava


1 Answers

From apple doc:

The session object keeps a strong reference to the delegate until your app exits or explicitly invalidates the session. If you don’t invalidate the session, your app leaks memory until it exits.

Also looking at your project https://github.com/upyun/swift-sdk/tree/testleak you need to call finishTasksAndInvalidate() after sessionTask.resume() since you are creating session per request

like image 180
Tomasz Czyżak Avatar answered Oct 07 '22 23:10

Tomasz Czyżak