以编程方式获取Chrome中的内存使用量

Pau*_*per 25 javascript performance google-chrome memory-profiling google-chrome-devtools

如何以编程方式在Google Chrome中获取我的网站的内存使用量(JS和总数)?

我看过使用未记录的HeapProfiler从Chrome扩展程序中执行此操作(请参阅此处),但我无法找到从中获取数据的方法.

我想测量每个版本的内存消耗,所以这需要编程.

编辑:我想出了如何使HeapProfiler方法工作.每个addHeapSnapshotChunk事件都有一个JSON对象的块.

chrome.browserAction.onClicked.addListener(function(tab) {
  var heapData,
    debugId = {tabId:tab.id};
  chrome.debugger.attach(debugId, '1.0', function() {    
    chrome.debugger.sendCommand(debugId, 'Debugger.enable', {}, function() {
      function headerListener(source, name, data) {
        if(source.tabId == tab.id && name == 'HeapProfiler.addProfileHeader') {
          function chunkListener(source, name, data) {
            if(name == 'HeapProfiler.addHeapSnapshotChunk') {
              heapData += data.chunk;
            } else if(name == 'HeapProfiler.finishHeapSnapshot') {
              chrome.debugger.onEvent.removeListener(chunkListener);
              chrome.debugger.detach(debugId);
              //do something with data
              console.log('Collected ' + heapData.length + ' bytes of JSON data');
            }
          }
          chrome.debugger.onEvent.addListener(chunkListener);
          chrome.debugger.sendCommand(debugId, 'HeapProfiler.getHeapSnapshot', {uid:data.header.uid, type:data.header.typeId});
        }
        chrome.debugger.onEvent.removeListener(headerListener);
      }
      chrome.debugger.onEvent.addListener(headerListener);
      chrome.debugger.sendCommand(debugId, 'HeapProfiler.takeHeapSnapshot');
    });
  });
});
Run Code Online (Sandbox Code Playgroud)

解析时,JSON具有节点,边和有关节点和边类型和字段的描述性元数据.

或者,如果我只想要总计,我可以使用时间轴事件.

那就是说,有没有比我在这里发现的更好的方法?

bch*_*rny 23

对于将来发现这一点的任何人,从Chrome版本20支持window.performance.memory,返回类似于:

{
  totalJSHeapSize: 21700000,
  usedJSHeapSize: 13400000,
  jsHeapSizeLimit: 1620000000
}
Run Code Online (Sandbox Code Playgroud)

  • 我一直在尝试这个,即使在创建 1,000,000+ 个键/值对象之前和之后,这些值对我来说也没有改变...... (2认同)

Sea*_*ean 10

另一种方法:编写指向此URL的网页抓取器:

铬://系统/

(注意:如果此URL再次更改,这是列出所有Chrome诊断页面的"主"URL:chrome:// chrome-urls /

该页面有一个"mem_usage"部分,提供内存使用情况的详细信息.

也许有一些方法可以将Chrome作为用户(例如在AutoIT或Python?中)编写脚本,在Chrome中加载此URL,然后按下Update按钮,然后解析JSON以获取您感兴趣的任何标签的内存使用情况.


其他方法:

  • 来自JavaScript - 使用window.performance

  • 来自JavaScript - 使用browser-report.js -

https://www.npmjs.com/package/browser-report