NSURLSession HTTP/2 memory leak

試著忘記壹切 提交于 2019-12-05 17:15:24

问题


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

回答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



来源:https://stackoverflow.com/questions/39409357/nsurlsession-http-2-memory-leak

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