为什么内存使用情况未正确更新? [英] Why is memory usage not correctly updated?

查看:80
本文介绍了为什么内存使用情况未正确更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

警告:该代码最终将耗尽浏览器标签的内存并使该标签崩溃.

Warning: this code will eventually exhaust your browser tab's memory and crash the tab.

在下面的小提琴中,我设置了一个过程,该过程会泄漏大量内存并不断报告正在使用的内存量.我正在使用performance.memory API,该API似乎仅在chrome上实现.问题在于,尽管内存使用量迅速增加,但报告的数量从未改变.相反,我希望最上面的数字会不断增加.

In the following fiddle, I have set up a process that leaks a lot of memory and continuously reports how much memory is being used. I am using the performance.memory API, which only seems to be implemented on chrome. The problem is that the amounts reported never change, despite the fact that memory usage is rapidly increasing. Instead, I would expect the number on top to continually increase.

这是JavaScript代码:

Here is the javascript code:

(function() {

var x = [];

function createSomeNodes() {
    var div,
        i = 100,
        frag = document.createDocumentFragment();
    for (;i > 0; i--) {
        div = document.createElement("div");
        div.appendChild(document.createTextNode(i + " - "+ new Date().toTimeString()));
        frag.appendChild(div);
    }
    document.getElementById("debug").appendChild(frag);
}

function clear() { document.getElementById('stats').innerHTML = ''; }

function show(stat) { 
  var div = document.getElementById('stats');
  div.appendChild(document.createTextNode(stat));
  div.appendChild(document.createElement("div"));
 }

var start = Date.now() + 2 * 1000;

function grow() {
    x.push(new Array(1000000).join('x'));
    createSomeNodes();
    setTimeout(grow,40);

if (Date.now() < start) { return; }

              (function() {
                clear();
                var pm = window.performance && window.performance.memory;
                if (!pm) {
show("no performance.memory api");
                  return;
                }
                var lim = pm.jsHeapSizeLimit, // Memory the JavaScript heap is limited to
                    // Memory allocated on the JavaScript heap (includes free space)
                    total = pm.totalJSHeapSize,
                    used = pm.usedJSHeapSize; // Memory currently being used 
                show("Crash Index (% of Heap Limit Allocated): " + Math.round(total / lim * 100));
                show("% of Allocated Memory Used: " + Math.round(used / total * 100));
              })();

}


    setTimeout(grow,1000);
})()

如果我将变量 start 设置为更高的数字,则我称之为崩溃索引"的 initial 值会更高,但在此之后是第一个如图所示,即使该值也不会增加.

If I set the variable start to a higher number, I see a higher initial value for what I have called the "crash index," but after it is first shown, even this value does not increase.

推荐答案

出于安全原因,存在一个限制,该限制会将Google Chrome浏览器中的内存采样次数限制为每20分钟一次.在Chrome 68+中,对于源自与网页(eTLD + 1)相同来源的脚本,该限制已减少为每50毫秒一次.这意味着您可能不再需要-enable-precise-memory-info .

There was a limitation that would restrict memory sampling to once every 20 minutes in Google Chrome for security reasons. In Chrome 68+ that limit has been reduced to once every 50ms for scripts served from the same origin as the webpage (eTLD+1). This means you probably don't need --enable-precise-memory-info anymore.

https://chromium.googlesource.com/chromium/src/+/7c7847f69de403f6c798dfccba10812039

这篇关于为什么内存使用情况未正确更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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