如何为 node.js 确定正确的 "max-old-space-size"?
How do I determine the correct "max-old-space-size" for node.js?
我在理解 Node.js 如何基于参数 max-old-space-size
时遇到一些问题。
例如,就我而言,我是 运行 两个 t2.small
AWS 实例(2GB 内存)。
不知道为什么,但我确实设置了 max-old-space-size=4096
(4GB)。节点在这种情况下做什么?此配置是否会导致内存分配失败?
如何根据服务器资源确定max-old-space-size
的正确值?
我的应用程序的内存使用量不断增加,我正试图了解有关节点内部的一切。
"Old space" 是 V8 托管(又名垃圾收集)堆(即 JavaScript 对象所在的位置)中最大且最可配置的部分,并且 --max-old-space-size
标志控制其最大尺寸。随着内存消耗接近极限,V8 将花费更多时间进行垃圾回收,以释放未使用的内存。
如果堆内存消耗(即 GC 无法释放的活动对象)超过限制,V8 将使您的进程崩溃(因为缺乏替代方案),因此您不想将其设置得太低。当然,如果你将它设置得太高,那么 V8 允许的额外堆使用可能会导致你的整个系统 运行 内存不足(并且交换或杀死随机进程,因为没有替代方案)。
总而言之,在具有 2GB 内存的机器上,我可能会将 --max-old-space-size
设置为大约 1.5GB,以便为其他用途留出一些内存并避免交换。
当分配给正在执行的应用程序的内存少于所需内存时,会发生此错误。
默认情况下,Node.js 中的内存限制为 512 MB。要增加此数量,您需要设置内存限制参数 —-max-old-space-size
。这将有助于避免内存限制问题。
node --max-old-space-size=1024 index.js #increase to 1gb
node --max-old-space-size=2048 index.js #increase to 2gb
node --max-old-space-size=3072 index.js #increase to 3gb
node --max-old-space-size=4096 index.js #increase to 4gb
node --max-old-space-size=5120 index.js #increase to 5gb
node --max-old-space-size=6144 index.js #increase to 6gb
node --max-old-space-size=7168 index.js #increase to 7gb
node --max-old-space-size=8192 index.js #increase to 8gb
https://medium.com/@vuongtran/how-to-solve-process-out-of-memory-in-node-js-5f0de8f8464c
2020更新
这些选项是now documented officially by node。对于 2GB 的机器,您可能应该使用:
NODE_OPTIONS=--max-old-space-size=1536
确定使用量
您可以使用 free -m
查看 Linux 机器上的可用内存。请注意,您可以将 free
和 buffers/cache
内存的总和视为可用,因为 buffers/cache
可以在需要时立即丢弃(这些缓冲区和缓存是一种很好的使用方式,否则未使用的内存)。
official documentation formax-old-space-size
还提到:
On a machine with 2GB of memory, consider setting this to 1536 (1.5GB)
因此,上面的值。考虑到基本 OS 所需的内存量变化不大,因此您可以愉快地在 4GB 机器上执行 3.5 等等
要注意默认值并查看更改的效果:
默认为2GB:
$ node
> v8.getHeapStatistics()
{
....
heap_size_limit: 2197815296,
}
2197815296 是 2GB 字节。
设置为8GB时,可以看到heap_size_limit
变化:
$ NODE_OPTIONS=--max_old_space_size=8192 node
Welcome to Node.js v14.17.4.
Type ".help" for more information.
> v8.getHeapStatistics()
{
...
heap_size_limit: 8640266240,
...
}
正如@Venryx 在下面提到的,您也可以使用 process.memoryUsage()
对我来说,当内存消耗接近 2GB 时,Azure 函数调用在 7GB 实例中终止,并出现以下错误消息。
Exception while executing function: Functions.graphql-mobile node exited with code 134
[24164:000001CE2EB5CF00] 10557962 ms: Scavenge (reduce) 2041.0 (2050.5) -> 2040.1 (2051.5) MB, 2.3 / 0.1 ms (average mu = 0.179, current mu = 0.002) allocation failure ,FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory, 3: 00007FF73E006026 node::OnFatalError+294
我通过在我的应用程序设置中像这样设置 max-old-space-size 解决了这个问题
"languageWorkers:node:arguments": "--max-old-space-size=5500"
我在理解 Node.js 如何基于参数 max-old-space-size
时遇到一些问题。
例如,就我而言,我是 运行 两个 t2.small
AWS 实例(2GB 内存)。
不知道为什么,但我确实设置了 max-old-space-size=4096
(4GB)。节点在这种情况下做什么?此配置是否会导致内存分配失败?
如何根据服务器资源确定max-old-space-size
的正确值?
我的应用程序的内存使用量不断增加,我正试图了解有关节点内部的一切。
"Old space" 是 V8 托管(又名垃圾收集)堆(即 JavaScript 对象所在的位置)中最大且最可配置的部分,并且 --max-old-space-size
标志控制其最大尺寸。随着内存消耗接近极限,V8 将花费更多时间进行垃圾回收,以释放未使用的内存。
如果堆内存消耗(即 GC 无法释放的活动对象)超过限制,V8 将使您的进程崩溃(因为缺乏替代方案),因此您不想将其设置得太低。当然,如果你将它设置得太高,那么 V8 允许的额外堆使用可能会导致你的整个系统 运行 内存不足(并且交换或杀死随机进程,因为没有替代方案)。
总而言之,在具有 2GB 内存的机器上,我可能会将 --max-old-space-size
设置为大约 1.5GB,以便为其他用途留出一些内存并避免交换。
当分配给正在执行的应用程序的内存少于所需内存时,会发生此错误。
默认情况下,Node.js 中的内存限制为 512 MB。要增加此数量,您需要设置内存限制参数 —-max-old-space-size
。这将有助于避免内存限制问题。
node --max-old-space-size=1024 index.js #increase to 1gb
node --max-old-space-size=2048 index.js #increase to 2gb
node --max-old-space-size=3072 index.js #increase to 3gb
node --max-old-space-size=4096 index.js #increase to 4gb
node --max-old-space-size=5120 index.js #increase to 5gb
node --max-old-space-size=6144 index.js #increase to 6gb
node --max-old-space-size=7168 index.js #increase to 7gb
node --max-old-space-size=8192 index.js #increase to 8gb
https://medium.com/@vuongtran/how-to-solve-process-out-of-memory-in-node-js-5f0de8f8464c
2020更新
这些选项是now documented officially by node。对于 2GB 的机器,您可能应该使用:
NODE_OPTIONS=--max-old-space-size=1536
确定使用量
您可以使用 free -m
查看 Linux 机器上的可用内存。请注意,您可以将 free
和 buffers/cache
内存的总和视为可用,因为 buffers/cache
可以在需要时立即丢弃(这些缓冲区和缓存是一种很好的使用方式,否则未使用的内存)。
official documentation formax-old-space-size
还提到:
On a machine with 2GB of memory, consider setting this to 1536 (1.5GB)
因此,上面的值。考虑到基本 OS 所需的内存量变化不大,因此您可以愉快地在 4GB 机器上执行 3.5 等等
要注意默认值并查看更改的效果:
默认为2GB:
$ node
> v8.getHeapStatistics()
{
....
heap_size_limit: 2197815296,
}
2197815296 是 2GB 字节。
设置为8GB时,可以看到heap_size_limit
变化:
$ NODE_OPTIONS=--max_old_space_size=8192 node
Welcome to Node.js v14.17.4.
Type ".help" for more information.
> v8.getHeapStatistics()
{
...
heap_size_limit: 8640266240,
...
}
正如@Venryx 在下面提到的,您也可以使用 process.memoryUsage()
对我来说,当内存消耗接近 2GB 时,Azure 函数调用在 7GB 实例中终止,并出现以下错误消息。
Exception while executing function: Functions.graphql-mobile node exited with code 134
[24164:000001CE2EB5CF00] 10557962 ms: Scavenge (reduce) 2041.0 (2050.5) -> 2040.1 (2051.5) MB, 2.3 / 0.1 ms (average mu = 0.179, current mu = 0.002) allocation failure ,FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory, 3: 00007FF73E006026 node::OnFatalError+294
我通过在我的应用程序设置中像这样设置 max-old-space-size 解决了这个问题
"languageWorkers:node:arguments": "--max-old-space-size=5500"