是否可以在无头镀铬中使用window.performance.memory?

时间:2016-06-28 22:45:15

标签: javascript google-chrome performance-testing

我想使用window.performance.memory测量我的无头镀铬测试中是否有任何内存泄漏。

在测试中

    beforeEach(() => {
        $('body').append(initHtml);
        console.log(window.performance.memory)
    });

结果:MemoryInfo {}

在开发者控制台中:

console.log(window.performance.memory)

结果: MemoryInfo {totalJSHeapSize: 27600000, usedJSHeapSize: 16100000, jsHeapSizeLimit: 1530000000}

我的Karma.conf包含以下内容:

browsers: ['Chrome_with_memory_tests'],
customLaunchers: {
    Chrome_with_memory_tests: {
        base: 'Chrome',
        flags: ['--enable-precise-memory-info']
    }
},

有谁知道为什么会这样?窗口对象可用,因为我可以在其上运行其他方法。也许我需要另一个标志来启用这个实验性功能? https://developer.mozilla.org/en-US/docs/Web/API/Window/performance

1 个答案:

答案 0 :(得分:0)

window.performance.memory的属性不可枚举

如果要查看它们,可以使用类似以下内容的

console.log(JSON.stringify(window.performance.memory, ['totalJSHeapSize', 'usedJSHeapSize', 'jsHeapSizeLimit']));

// Will output something like :
// {"totalJSHeapSize":239000000,"usedJSHeapSize":225000000,"jsHeapSizeLimit":2330000000}

实际上,您确实必须启用enable-precise-memory-info标志才能在无头模式下执行程序期间正确更新值。