我在使用Javascript中的websocket时遇到问题。 内存消耗不断增加,我只是不明白为什么。
我的websocket服务器每秒发送大约1KB的数据,而且消耗的内存似乎以相同的速率增加。
如果我打开Chrome任务管理器,我可以看到我的标签消耗的内存不断增加。它似乎与Internet Explorer存在同样的问题。
我的代码基于此页面上的示例:https://www.tutorialspoint.com/html5/html5_websocket.htm
这个问题似乎有些类似,但我仍然不明白问题所在。 Memory leak in Chrome with Websockets
我做错了什么?
刷新网页会释放内存但必须有其他方式...
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
var systems = {};
function WebSocketTest()
{
if ("WebSocket" in window)
{
// Let us open a web socket
var ws = new WebSocket("ws://192.168.1.1:8081/websocket");
ws.onopen = function()
{
// Web Socket is connected, send data using send()
var subscribe = {};
var request = {};
request["test"] = "1";
subscribe["subscribe"] = request;
ws.send(JSON.stringify(subscribe));
//alert("Message is sent...");
};
ws.onmessage = function (evt)
{
var received_msg = evt.data;
var obj = JSON.parse(received_msg);
var samples = obj.samples;
delete samples;
delete obj;
delete receieved_msg;
};
ws.onclose = function()
{
// websocket is closed.
alert("Connection is closed...");
};
window.onbeforeunload = function(event) {
socket.close();
};
}
else
{
// The browser doesn't support WebSocket
alert("WebSocket NOT supported by your Browser!");
}
}
</script>
</head>
<body>
<a href="javascript:WebSocketTest()">Run WebSocket</a>
</div>
</body>
<script>
</script>
</html>