title: Programmatically get memory usage in Chrome
body: <p>How can I programmatically get memory usage (JS and total) of my website in Google Chrome?</p>
  
  <p>I looked at doing it from a Chrome extension using the undocumented HeapProfiler (see <a href="https://stackoverflow.com/questions/17517681/chrome-debugging-protocol-heapprofiler-getheapsnapshot-ignores-callback">here</a>), but I can't find a way to get data from that.</p>
  
  <p>I want to measure the memory consumption it at every release, so this needs to be programmatic.</p>
  
  <p>EDIT: I figured out how to get the HeapProfiler method to work. Each <code>addHeapSnapshotChunk</code> event has a chunk of a JSON object.</p>
  
  <pre><code>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 &amp;&amp; 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');
      });
    });
  });
  </code></pre>
  
  <p>When parsed, the JSON has nodes, edges, and descriptive metadata about the node and edge types and fields.</p>
  
  <p>Alternatively, I could use <a href="https://developers.google.com/chrome-developer-tools/docs/protocol/1.0/timeline" rel="noreferrer">Timeline</a> events if I just want totals.</p>
  
  <p>That said, are there any better ways than what I've found out here?</p>