如何增加 Node.js 中的最大调用堆栈大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11332422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:58:33  来源:igfitidea点击:

How can I increase the maximum call stack size in Node.js

node.jsstack-overflowcallstack

提问by thomas-peter

This is different to other questions regarding an error message in Node that reads RangeError: Maximum call stack size exceededin that I know exactly why I'm getting this error message. It's happening because I'm recursing, recursing quite a lot in fact.

这与 Node 中有关读取RangeError: Maximum call stack size exceeded的错误消息的其他问题不同,因为我确切地知道为什么会收到此错误消息。之所以发生这种情况,是因为我在递归,实际上递归了很多。

Thanks.

谢谢。

回答by JohnnyHK

From node --help:

来自node --help

node --max-stack-size=val

Update: as the comments indicate, even though the help text still lists the --max-stack-sizeoption, in node v0.10.x you need to use --stack-sizeinstead.

更新:正如评论所示,即使帮助文本仍然列出了该--max-stack-size选项,在节点 v0.10.x 中您需要改为使用--stack-size

node --stack-size=val

回答by Florent Angly

In node version 5 and 6, I have verified that the option to set maximum stack size is "--stack_size" (with an underscore):

在节点版本 5 和 6 中,我已验证设置最大堆栈大小的选项为“--stack_size”(带下划线):

$  node --v8-options
[...]
--stack_size (default size of stack region v8 is allowed to use (in kBytes))
      type: int  default: 984

To increase the maximum stack size, just issue something like:

要增加最大堆栈大小,只需发出如下命令:

$ node --stack_size=1200

As noted by others, be aware that increasing this value may lead to a segmentation fault. The maximum safe value for me with version 6 is 1361, but seems higher with version 5.

正如其他人所指出的,请注意增加此值可能会导致分段错误。对我来说,版本 6 的最大安全值为 1361,但版本 5 似乎更高。

Looking at the bigger picture, increasing stack size may not solve all your issues. When writing recursive functions in node, your best strategy is to write them in a tail-recursivemanner, since version 6 supports proper tail calls. This will eliminate stack size overflows.

从更大的角度来看,增加堆栈大小可能无法解决您的所有问题。在 node 中编写递归函数时,最好的策略是以尾递归方式编写它们,因为版本 6 支持正确的尾调用。这将消除堆栈大小溢出。