检索更新的 JS 堆内存大小

Retrieve updated JS Heap memory size

我正在开发一个记录用户屏幕的网络应用程序。它适用于少于 20 分钟(估计)的截屏视频。但是 chrome 在上述时间后崩溃。

我想知道是否可以获取我的应用程序消耗的更新堆大小。我的应用程序仅适用于 chrome.

我试过代码:

window.performance.memory

它每次都给我以下结果:

MemoryInfo {
 jsHeapSizeLimit: 793000000, 
 usedJSHeapSize: 10000000,
 totalJSHeapSize: 31200000
}

usedJsHeapSize 是包括 V8 内部对象在内的 JS 对象正在使用的内存总量

totalJsHeapSize 是 JS 堆的当前大小,包括未被任何 JS 对象占用的空闲 space。这意味着 usedJsHeapSize 不能大于 totalJsHeapSize。

请注意,并不一定曾经有过totalJsHeapSize个活着的JS对象。

我开始记录,usedJSHeapSize 的大小必须在堆中更新。 如何在 javascript 或 jquery 中获取更新后的 usedJSHeapSize?

window.performance.memory

以上代码一次又一次给出相同的结果。下面我来演示一下:

<script>
console.log("JS Heap size: " + window.performance.memory.jsHeapSizeLimit);
console.log("usedJSHeapSize: " + window.performance.memory.usedJSHeapSize);

var x = [];
for(var i=0; i<12345678; i++) {
    x.push(i);
}


console.log("after some calculations usedJSHeapSize: " + window.performance.memory.usedJSHeapSize);
</script>

结果:

JS Heap size: 1620000000
usedJSHeapSize: 10000000
after some calculations usedJSHeapSize: 10000000

Chrome 不允许我们获取更新后的堆大小。它 returns 一次又一次地使用相同的值。