在 Node.js 中的内存限制(和 chrome V8)

在网络的许多地方,你会看到:

节点进程的内存限制是多少?

答案是:

目前,默认情况下,V8在32位系统上的内存限制为512mb,在64位系统上的内存限制为1gb。这个限制可以通过设置—— max-old-space-size 到 ~ 1gb (32位)和 ~ 1.7 gb (64位)的最大值来提高,但是如果你达到了内存限制,建议你把你的单个进程分成几个工作进程。

有人能证实 Node.js 经常更新的情况吗?

更重要的是,在不久的将来会是这样吗

我想写的 JavaScript 代码可能要处理4gb 的 JavaScript 对象(速度可能不是问题)。

如果我不能在 Node 中完成,那么我将最终在 java 中完成(在64位机器上) ,但我宁愿不这样做。

129959 次浏览

It looks like it's true. When I had tried to allocate 50 Mb string in buffer:

var buf = new Buffer(50*1024*1024);

I've got an error:

FATAL ERROR: CALL_AND_RETRY_2 Allocation failed - process out of memory

Meantime there was about 457 Mb of memory usage by Node.js in process monitor.

This has been a big concern for some using Node.js, and there are good news. The new memory limit for V8 is now unknown (not tested) for 64bit and raised to as much as 32bit address space allows in 32bit environments.

Read more here: http://code.google.com/p/v8/issues/detail?id=847

I'm running a proc now on Ubuntu linux that has a definite memory leak and node 0.6.0 is pushing 8gb. Think it's handled :).

Starting nodejs app with a heap memory of 8 GB

node --max-old-space-size=8192 app.js

See node command line options documentation or run:

node --help --v8-options

Memory Limit Max Value is 3049 for 32bit users

If you are running Node.js with os.arch() === 'ia32' is true, the max value you can set is 3049

under my testing with node v11.15.0 and windows 10

  • if you set it to 3050, then it will overflow and equal to be set to 1.
  • if you set it to 4000, then it will equal to be set to 51 (4000 - 3049)
Set Memory to Max for Node.js
node --max-old-space-size=3049
Set Memory to Max for Node.js with TypeScript
node -r ts-node/register --max-old-space-size=3049

See: https://github.com/TypeStrong/ts-node/issues/261#issuecomment-402093879