Node.js单个对象的堆内存限制

 Angkaka 发布于 2022-12-28 20:02

v8是否对单个对象的堆分配有限制?

a = new Array(1024*1024*102)

在节点命令行上失败

FATAL ERROR: JS Allocation failed - process out of memory

此外,当作为脚本运行时,这会失败并出现相同的错误

node --expose-gc --nouse-idle-notification --max-old-space-size=8192

FATAL ERROR: CALL_AND_RETRY_0 Allocation failed - process out of memory

var util = require('util');
o = {};

while(1) {
    o["hahahahahaha" + String(ctr)] = ctr;
    ctr++;

    if (ctr % 100000 === 0) {
        console.log(util.inspect(process.memoryUsage()));
        if (ctr % 10000000 === 0) gc();
    }
}

最后输出:

{ rss: 1009557504, heapTotal: 993408824, heapUsed: 964980592 }

然而,

var a = [];
while(1) {
    var o = {};
    o["hahahahahaha" + String(ctr)] = ctr;
    a.push(o);
    ctr++;

    if (ctr % 100000 === 0) {
        console.log(ctr);
        console.log(util.inspect(process.memoryUsage()));
        console.log();
        if (ctr % 10000000 === 0) gc();
    }
}

很好

{ rss: 5466140672, heapTotal: 1091224368, heapUsed: 1070460592 }

编辑:

node -v

v0.10.25

uname -a

Linux 3.13.0-24-generic #47-Ubuntu SMP Fri May 2 23:30:00 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

编辑2:即便如此!似乎v8的限制适用于对象可以拥有的属性数量?

while(1) {

    if (!o["hahahahahaha" + String(Math.floor(ctr/1000000))]) {
        o["hahahahahaha" + String(Math.floor(ctr/1000000))] = {};
        console.log(Object.keys(o))
    }
    o["hahahahahaha" + String(Math.floor(ctr/1000000))][String(ctr)] = ctr;
    ctr++;

    if (ctr % 100000 === 0) {
        console.log(ctr);
        console.log(util.inspect(process.memoryUsage()));
        console.log();
        if (ctr % 10000000 === 0) gc();
    }
}

{ rss: 2474512384, heapTotal: 2466405768, heapUsed: 2431583008 }

另外,我发现了这个:https: //github.com/v8/v8/blob/master/src/objects.h#L2928

我想知道它是否相关.

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有