如何在nodejs中证明v8的默认最大堆限制,或者方法“ process.memoryUsage()”准确显示总堆?

时间:2019-05-22 04:52:58

标签: node.js memory v8

我读了一本书,它说v64的默认最大旧版本在x64中为1.4G, 使用方法“ process.memoryUsage()”显示内存使用情况, 作者通过一些代码证明了这一点,将Array对象重复到一个全局变量中。 几次后,发生内存不足。 当我写了一些不同的代码时,重复将一些字符串写到文件中, 这个限制很快就被打破了,那么我们的代码在内存使用上有什么区别呢? 方法“ process.memoryUsage()”是否确实显示了内存使用情况? (我的操作系统窗口为x64、16G内存,节点8,节点选项为默认) 任何帮助将不胜感激!!!

这是作者的代码:

outofmemory.js

var showMem = function (times) {
    var mem = process.memoryUsage();
    var format = function (bytes) {
        return (bytes / 1024 / 1024).toFixed(2) + ' MB';
    };
    console.log('Process: heapTotal ' + format(mem.heapTotal) +
        ' heapUsed ' + format(mem.heapUsed) + ' rss ' + format(mem.rss));
    console.log(`--------------------------${times}---------------------------------`);
};

var useMem = function () {
    var size =  20*1024 * 1024;
    var arr = new Array(size);
    for (var i = 0; i < size; i++) {
        arr[i] = 0;
    }
    return arr;
};
var total = [];
for (var j = 0; j < 10000; j++) {
    showMem(j);
    total.push(useMem());
}
showMem();

投入量

...
Process: heapTotal 1286.92 MB heapUsed 1284.28 MB rss 1301.80 MB
--------------------------8---------------------------------

<--- Last few GCs --->

[15468:00000267A6225110]     1074 ms: Mark-sweep 1284.0 (1290.9) -> 1283.9 (1290.9) MB, 119.4 / 0.0 ms  allocation failure GC in old space requested
[15468:00000267A6225110]     1195 ms: Mark-sweep 1283.9 (1290.9) -> 1283.9 (1287.9) MB, 120.7 / 0.0 ms  last resort GC in old space requested
[15468:00000267A6225110]     1314 ms: Mark-sweep 1283.9 (1287.9) -> 1283.9 (1287.9) MB, 118.6 / 0.0 ms  last resort GC in old space requested

...

这是我的:

const fs = require('fs');

async function writeFile(){
    const fd =  fs.openSync('a.log', 'a');
    const log = async (entry) =>{
        await fs.writeSync(fd, entry+'\r\n', null, 'utf8');
        await fs.fsyncSync(fd);
    }

    for(let t = 0; t<10000 ; t++ ) {
        for(var i = 0x4E00;i<=0x9FBF;++i)
            log(String.fromCharCode(i));
        showMem({times:t});
    }
}

const showMem = function ({times}) {
    const mem = process.memoryUsage();
    const format = function (bytes) {
        return (bytes / 1024 / 1024).toFixed(2) + ' MB';
    };

    console.log('Process: heapTotal ' + format(mem.heapTotal) +
        ' heapUsed ' + format(mem.heapUsed) + ' rss ' + format(mem.rss));
    console.log(`-----------------------------${times}--------------------------------\r\n`)
}
writeFile();

输出:

...
Process: heapTotal 2733.34 MB heapUsed 2655.47 MB rss 2756.39 MB
-----------------------------199--------------------------------


<--- Last few GCs --->

[24568:000001E7950D8B40]   380916 ms: Mark-sweep 2660.1 (2748.3) -> 2660.0 (2748.8) MB, 2900.9 / 0.0 ms  allocation failure GC in old space requested
[24568:000001E7950D8B40]   383390 ms: Mark-sweep 2660.0 (2748.8) -> 2660.0 (2715.3) MB, 2473.2 / 0.0 ms  last resort GC in old space requested
[24568:000001E7950D8B40]   385803 ms: Mark-sweep 2660.0 (2715.3) -> 2660.0 (2712.3) MB, 2411.6 / 0.0 ms  last resort GC in old space requested
...

1 个答案:

答案 0 :(得分:0)

最后,我一个人得到

打印的“ heapTotal,heapUsed”包括旧一代和新一代,它们两者都可以通过节点选项设置:

(CentOS 7.3 x64 4G)

 node --max-old-space-size=100  --max_semi_space_size=100  wf.js