带有 Websockets 的 Chrome 中的内存泄漏 [英] Memory leak in Chrome with Websockets

查看:51
本文介绍了带有 Websockets 的 Chrome 中的内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在通过 websockets 将二进制数据从我们的服务器传输到网络浏览器.浏览器接收数据就好了;我们可以用 Javascript 来操作它.

We are streaming binary data from our server via websockets to web browsers. The browser receives the data just fine; we can manipulate it with Javascript.

代码很基础:

   function WebSocketTest()
   {
        if ("WebSocket" in window)
        {
           var ws = new WebSocket("ws://localhost:3456/someresource");

           ......

           ws.onmessage = function (evt) 
           { 
              //var received_msg = evt.data;
              //var reader = new window.FileReader();
              //...........
           };

           ..............................................
           ..............................................

问题是——即使在这种最简单的形式中(onmessage 函数都是空的,所以我们对 evt.data 不做任何处理,这是一个二进制 blob),Chrome 浏览器的内存使用量一直在增长,就好像垃圾收集器永远不会释放一样斑点.它会一直增长,直到 Chrome 关闭 websocket 连接或崩溃.我们尝试过使用各种方法手动释放 evt.data,但没有任何帮助.您会期望上面的代码不会导致 Chrome 中的内存泄漏,对吗?如何释放evt.data引用的blob?

The problem is - even in this simplest form (onmessage function is all empty, so we do nothing with evt.data, which is a binary blob), the memory usage of Chrome browser keeps growing, as if garbage collector never frees the blob. It grows forever until Chrome closes websocket connection or crashes. We have tried to manually release evt.data with various methods, but nothing helps. You would expect that the code above will not cause memory leak in Chrome, correct? How to release the blob referenced by evt.data?

推荐答案

嘿,我找到了内存泄漏"的原因——这不是内存泄漏,而是我的编程错误.在这里发布以防万一其他人犯同样的错误.发生的事情是 - 对于大于 64KB 的消息,您需要将数据长度格式化为以网络字节顺序打包的 64 位整数.我没有正确地将基于 Windows 的 __int64 转换为网络字节顺序.所以浏览器认为它正在下载一个巨大的消息(10GB 或类似的东西) - 并继续下载直到内存用完.

Hey I have found the reason for "memory leak" - it's not a memory leak but my programming error. Posting here just in case anyone else makes the same mistake. What happened is - for messages larger than 64KB you need to format the data length as a 64-bit integer packaged in network byte order. I was not converting my Windows-based __int64 to network byte order correctly. So the browser thought it's downloading a huge message (10GB or something like that) - and kept downloading until run out of memory.

这篇关于带有 Websockets 的 Chrome 中的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆