gpt4 book ai didi

javascript - 来自 PubNub Javascript SDK 的内存泄漏

转载 作者:行者123 更新时间:2023-11-30 00:23:01 25 4
gpt4 key购买 nike

我正在使用 pubnub 来处理我正在处理的应用程序前端的实时更新。用户一直在经历很多浏览器崩溃,特别是在 windows chrome 上。我运行了 chrome profiler,发现 pubnub 不断地创建事件监听器,直到浏览器最终运行我们的内存并崩溃。有没有办法手动清理由 pubnub 创建的事件监听器或任何其他解决方法?

编辑我也尝试过切换到 websockets 版本,但问题变得更糟。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Hello Punub</title>
<script src="https://pubnub.a.ssl.fastly.net/pubnub.min.js"></script>
</head>
<body>

<h1>Hello</h1>
</body>

<!-- Import PubNub Core Lib -->

<!-- Use WebSocket Constructor for a New Socket Connection -->
<script>(function() {
/* 'wss://ORIGIN/PUBLISH_KEY/SUBSCRIBE_KEY/CHANNEL' */
WebSocket = PUBNUB.ws;
var socket = new WebSocket('wss://pubsub.pubnub.com/PUB/SUB/CHANNEL')
// On Message Receive
socket.onmessage = function(evt) {
console.log('socket receive');
console.log(evt.data);
}
// On Socket Close
socket.onclose = function() {
console.log('socket closed');
}
// On Error
socket.onerror = function() {
console.log('socket error');
}
// On Connection Establish
socket.onopen = function(evt) {
console.log('socket open');
// Send a Message!
socket.send('hello world!');
}
// On Send Complete
socket.onsend = function(evt) {
console.log('socket send');
console.log(evt);
}
console.log(socket)
})();</script>
</html>

Chrome Profiler

EDIT 2 我已经使用以下代码重新运行分析器

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Hello Punub</title>
<script src="https://pubnub.a.ssl.fastly.net/pubnub.min.js"></script>
</head>
<body>

<h1>Hello</h1>
</body>

<!-- Import PubNub Core Lib -->

<!-- Use WebSocket Constructor for a New Socket Connection -->
<script>(function() {
/* 'wss://ORIGIN/PUBLISH_KEY/SUBSCRIBE_KEY/CHANNEL' */
WebSocket = PUBNUB.ws;
var socket = new WebSocket('wss://pubsub.pubnub.com/PUB/SUB/CHANNEL')
// On Message Receive
socket.onmessage = function(evt) {

}
// On Socket Close
socket.onclose = function() {

}
// On Error
socket.onerror = function() {

}
// On Connection Establish
socket.onopen = function(evt) {
// Send a Message!
socket.send('hello world!');
}
// On Send Complete
socket.onsend = function(evt) {

}
})();</script>
</html>

enter image description here

最佳答案

使用 PubNub JavaScript SDK 测试系统利用率和内存配置文件

您正在使用浏览器调试控制台测量 JS 堆。很不错!这没关系,但你必须做出调整。您在测试中要解决的一个重要问题是注意 console.log(...) 会导致内存膨胀。这是一个常见问题 https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=console.log%20memory%20usage -

对于 future 的测试,请确保在代码中所有地方 排除所有console.log() 执行。

Your EDIT from above includes several more console.log() lines which will emphasize the memory balloon effect.

内存分析更新

我目前正在 Chrome 网络浏览器中使用基于您原始 stackoverflow 帖子的相同时间线内存监控方法运行以下测试。

<script src="https://cdn.pubnub.com/pubnub-dev.js"></script>
<script>(function(){

WebSocket = PUBNUB.ws;
var socket = new WebSocket('wss://pubsub.pubnub.com/demo/demo/CHANNEL')

// On Message Receive
socket.onmessage = function(evt) {}

// On Socket Close
socket.onclose = function() {}

// On Error
socket.onerror = function() {}

// On Connection Establish
socket.onopen = function(evt) {}

// On Send Complete
socket.onsend = function(evt) {}

})();</script>

正如您从下图中看到的,启用了内存分析的浏览器Timeline 选项卡的配置文件应该如下所示。

浏览器上内存配置文件 WebSocket JavaScript 的结果

JavaScript Heap GC will trigger periodically claiming used memory.

Memory Profiling WebSockets

JavaScript Listeners are an always increasing integer that reset to zero periodically.

JavaScript Listeners Increase and Start Fresh

在内存分析 JavaScript 时关闭关闭 console.log()

此处的关键部分是您从测试中排除console.log,因为您将得到以下错误结果!

<script>
setInterval(function(){
console.log({a:document,b:window,c:document.body})
},100)
</script>

Console Log Problem with JavaScript Memory Profiling

console.log Problem with JavaScript Memory Profiling efforts.

关于javascript - 来自 PubNub Javascript SDK 的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32484373/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com