谁能解释一下为什么下面显示的两个示例之间的内存占用量似乎存在如此大的差异?我使用 node.js 作为环境。
它们都产生相同的结果和映射
var util = require('util');
var startMem = process.memoryUsage().heapUsed;
/*
var test = function() {
var testvar = function() {
this.innerTest1 = function() {
this.someVal1 = "this is a test";
this.someFunc1 = function() {
return "this is another test";
}
}
this.innerTest2 = function() {
this.someVal2 = "this is a third test";
this.someFunc2 = function() {
return "this is a forth test";
}
}
}
}
*/
//out:
//mem: 448088
var test = function() {
var testvar = {
innerTest1 : {
someVal1 : "this is a test",
someFunc1 : function() {
return "this is another test"
}
},
innerTest2 : {
someVal2 : "this is a third test",
someFunc2 : function() {
return "this is a forth test"
}
}
}
}
for (var i = 0; i < 50000; i++) {
var testobj = new test();
}
//out:
//mem: 1060040
console.log("mem: " + (process.memoryUsage().heapUsed - startMem));