NSURLSession HTTP / 2内存泄漏 [英] NSURLSession HTTP/2 memory leak
问题描述
这个My Test案例指出,当使用带有HTTP / 2连接的NSURLSession时,存在内存问题。
This My Test cases, point out that when using NSURLSession with a HTTP/2 connection there is memory problem.
test1:iOS 9. HTTP / 2服务器
我使用NSURLSession将10M文件上传到HTTP / 2服务器,如果上传的文件完成一切正常,但如果我取消上传任务完成之前,10M永远不会释放。
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服务器
我用https1.1文件服务器测试相同的代码,我是否取消上传任务,一切正常,内存恢复正常。(10M数据发布)
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服务器
这种情况一切正常。(NSURLSession没有协议谈判到HTTP / 2)
This case everything is ok.(NSURLSession did not protocol negotiation to HTTP/2)
所以,即使有一些不适合我使用NSURLSession的事情,NSURLSession的性能也不适用于HTTP / 2.
So, Even there is some thing not appropriate with my using NSURLSession, NSURLSession performance is not normal with HTTP/2.
除了内存问题,使用带有HT的NSURLSession TP / 2到上传文件的进度段大小很大(5月2M''didSendBodyData'在一次回拨时)
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)
我也读过此页面。 SSL可以缓存一些东西,但不应该缓存整个文件。(当我取消任务或请求超时时,10M文件大小的内存泄漏)
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.
问题更新0912:添加测试项目链接
测试项目: 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
推荐答案
来自apple doc:
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.
同时查看你的项目 https://github.com/upyun/swift-sdk/tree/testleak 你需要打电话在sessionTask.resume()之后的finishTasksAndInvalidate(),因为您正在为每个请求创建会话
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
这篇关于NSURLSession HTTP / 2内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!