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

标签 javascript node.js memory-management v8

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

a = new Array(1024*1024*102)

Node 命令行失败

fatal error :JS 分配失败 - 进程内存不足

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

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

fatal error :CALL_AND_RETRY_0 分配失败 - 进程内存不足

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

我想知道它是否相关。

最佳答案

事实证明,字符串、对象和数组的最大大小都有硬性限制。 限制是旧垃圾收集器的残余。 这是相关的票证:

https://code.google.com/p/v8/issues/detail?id=3505

关于javascript - 单个对象的 Node.js 堆内存限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25231808/

相关文章:

javascript - 使用 Body.json() Promise 处理非 JSON 响应

javascript - 如何在 JavaScript 中使用 href

javascript - 仅在数据不存在时将数据添加到数据库(Node js)

C++:关于释放内存的问题

javascript - D3.js 比例 : Return the smaller of a constant and the value of a scale?

javascript - 如何将日期范围分配给数组并与日期输入数据进行比较

javascript - 如何使用 r.js 优化 ember-data

node.js - 由于异步调用,Mongoose 检索到的数据为空

c - opencv抓包导致内存分配错误

c# - 使用 SQL Server CE 的内存/性能