【问题标题】:Retrieve updated JS Heap memory size检索更新的 JS 堆内存大小
【发布时间】:2016-05-20 09:57:38
【问题描述】:

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

我想知道我的应用程序是否可以获取更新后的堆大小。我的应用程序只能在 chrome 中运行。

我试过代码:

window.performance.memory

它每次都会给我以下结果:

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

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

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

请注意,活跃的 JS 对象不一定有 totalJsHeapSize。

我开始记录,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

【问题讨论】:

  • 我的应用程序可以消耗多少内存来消耗 javascript 中的堆内存。它可以使用任何计算吗?

标签: javascript jquery angularjs memory profiling


【解决方案1】:

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

【讨论】:

  • 您必须设置 chrome 选项并将 --enable-precise-memory-info 添加到它们
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-23
  • 2020-01-24
  • 1970-01-01
  • 1970-01-01
  • 2013-01-16
  • 1970-01-01
相关资源
最近更新 更多