NSURLSession HTTP/2 memory leak
Asked Answered
H

1

10

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
Hett answered 9/9, 2016 at 10:22 Comment(3)
Post your code.Kalinin
Has add a link to a test project and edited the origin question. Thanks for your time. @KalininHett
I recommend you use uploadTaskWithStreamedRequest for large uploadsAmbassador
F
1

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

Freda answered 6/10, 2017 at 20:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.