在Chrome中以编程方式获取内存使用量 [英] Programmatically get memory usage in Chrome
问题描述
我如何以编程方式在Google Chrome中获取我网站的内存使用量(JS和总数)?
我使用未公开的HeapProfiler (请参阅此处),但我无法找到一种方法从中得到数据。
我想测量每个版本的内存消耗,所以这需要编程。
编辑:我想出了如何让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(){
函数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);
//用数据做
console.log('已收集'+ heapDa ta.length +'JSON数据的字节');
}
}
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');
});
});
});
解析后,JSON具有节点,边缘和关于节点和边缘类型和字段的描述性元数据。
或者,我可以使用时间线事件,如果我只是想要总数。
也就是说,有没有比我在这里发现的更好的方法对于任何未来会发现这一点的人来说,自20版Chrome支持 .webplatform.org / wiki / apis / timing / properties / memoryrel =noreferrer> window.performance.memory
,它返回如下内容:
{
totalJSHeapSize:21700000,
usedJSHeapSize:13400000,
jsHeapSizeLimit:1620000000
}
How can I programmatically get memory usage (JS and total) of my website in Google Chrome?
I looked at doing it from a Chrome extension using the undocumented HeapProfiler (see here), but I can't find a way to get data from that.
I want to measure the memory consumption it at every release, so this needs to be programmatic.
EDIT: I figured out how to get the HeapProfiler method to work. Each addHeapSnapshotChunk
event has a chunk of a JSON object.
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');
});
});
});
When parsed, the JSON has nodes, edges, and descriptive metadata about the node and edge types and fields.
Alternatively, I could use Timeline events if I just want totals.
That said, are there any better ways than what I've found out here?
For anyone that finds this in the future, since version 20 Chrome supports window.performance.memory
, which returns something like:
{
totalJSHeapSize: 21700000,
usedJSHeapSize: 13400000,
jsHeapSizeLimit: 1620000000
}
这篇关于在Chrome中以编程方式获取内存使用量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!