Java 堆栈溢出错误 - 如何在 Eclipse 中增加堆栈大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2127217/
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-08-13 03:51:12  来源:igfitidea点击:

Java stack overflow error - how to increase the stack size in Eclipse?

javaeclipsejvmstack-overflowjvm-arguments

提问by tree-hacker

I am running a program that I've written in Java in Eclipse. The program has a very deep level of recursion for very large inputs. For smaller inputs the program runs fine however when large inputs are given, I get the following error:

我正在运行我在 Eclipse 中用 Java 编写的程序。对于非常大的输入,该程序具有非常深的递归级别。对于较小的输入,程序运行良好,但是当给出较大的输入时,我收到以下错误:

Exception in thread "main" java.lang.StackOverflowError

Can this be solved by increasing the Java stack size and if so, how do I do this in Eclipse?

这可以通过增加 Java 堆栈大小来解决吗?如果是这样,我如何在 Eclipse 中执行此操作?

Update:

更新:

@Jon Skeet

@乔恩斯基特

The code is traversing a parse tree recursively in order to build up a datastructure. So, for example the code will do some work using a node in the parse tree and call itself on the node's two children, combining their results to give the overall result for the tree.

代码递归地遍历解析树以构建数据结构。因此,例如,代码将使用解析树中的一个节点做一些工作,并在该节点的两个子节点上调用自身,将它们的结果组合起来给出树的整体结果。

The total depth of the recursion depends on the size of the parse tree but the code seems to fail (without a larger stack) when the number of recursive calls gets into the 1000s.

递归的总深度取决于解析树的大小,但是当递归调用的数量达到 1000 秒时,代码似乎失败了(没有更大的堆栈)。

Also I'm pretty sure the code isn't failing because of a bug as it works for small inputs.

此外,我很确定代码不会因为错误而失败,因为它适用于小输入。

采纳答案by Andreas Dolk

Open the Run Configurationfor your application (Run/Run Configurations..., then look for the applications entry in 'Java application').

打开应用程序的运行配置(运行/运行配置...,然后在“Java 应用程序”中查找应用程序条目)。

The argumentstab has a text box Vm arguments, enter -Xss1m(or a bigger parameter for the maximum stack size). The default value is 512 kByte (SUN JDK 1.5 - don't know if it varies between vendors and versions).

参数选项卡中有一个文本框VM参数,输入-Xss1m(或最大堆栈大小更大参数)。默认值为 512 kByte(SUN JDK 1.5 - 不知道它是否因供应商和版本而异)。

回答by Gordon

Add the flag -Xss1024kin the VM Arguments.

-Xss1024k在 VM 参数中添加标志。

You can also increase stack size in mbby using -Xss1mfor example .

您还可以mb通过使用-Xss1m例如增加堆栈大小。

回答by Jon Skeet

It maybe curable by increasing the stack size - but a bettersolution would be to work out how to avoid recursing so much. A recursive solution can always be converted to an iterative solution - which will make your code scale to larger inputs much more cleanly. Otherwise you'll really be guessing at how much stack to provide, which may not even be obvious from the input.

可能可以通过增加堆栈大小来解决- 但更好的解决方案是找出如何避免递归这么多。递归解决方案始终可以转换为迭代解决方案 - 这将使您的代码更清晰地扩展到更大的输入。否则你真的会猜测要提供多少堆栈,这甚至可能从输入中看不出来。

Are you absolutely sure it's failing due to the size of the input rather than a bug in the code, by the way? Just how deep is this recursion?

顺便说一下,你绝对确定它是由于输入的大小而不是代码中的错误而失败的吗?这个递归有多深?

EDIT: Okay, having seen the update, I would personally try to rewrite it to avoid using recursion. Generally having a Stack<T>of "things still do to" is a good starting point to remove recursion.

编辑:好的,看到更新后,我个人会尝试重写它以避免使用递归。通常有一个Stack<T>“仍然要做的事情”是消除递归的一个很好的起点。

回答by Thorbj?rn Ravn Andersen

You need to have a launch configuration inside Eclipse in order to adjust the JVM parameters.

您需要在 Eclipse 中进行启动配置才能调整 JVM 参数。

After running your program with either F11 or Ctrl-F11, open the launch configurations in Run -> Run Configurations... and open your program under "Java Applications". Select the Arguments pane, where you will find "VM arguments".

使用 F11 或 Ctrl-F11 运行您的程序后,在“运行”->“运行配置...”中打开启动配置,然后在“Java 应用程序”下打开您的程序。选择“参数”窗格,您将在其中找到“VM 参数”。

This is where -Xss1024kgoes.

这是-Xss1024k去的地方。

If you want the launch configuration to be a file in your workspace (so you can right click and run it), select the Common pane, and check the Save as -> Shared File checkbox and browse to the location you want the launch file. I usually have them in a separate folder, as we check them into CVS.

如果您希望启动配置是工作区中的文件(以便您可以右键单击并运行它),请选择公共窗格,然后选中另存为 -> 共享文件复选框并浏览到您想要启动文件的位置。我通常将它们放在一个单独的文件夹中,因为我们将它们检入 CVS。

回答by Ratnakar

i also have the same problem while parsing schema definition files(XSD) using XSOM library,

我在使用 XSOM 库解析模式定义文件(XSD)时也遇到了同样的问题,

i was able to increase Stack memory upto 208Mb then it showed heap_out_of_memory_errorfor which i was able to increase only upto 320mb.

我能够将堆栈内存增加到 208Mb,然后它显示heap_out_of_memory_error我只能增加到 320MB。

the final configuration was -Xmx320m -Xss208mbut then again it ran for some time and failed.

最终的配置是,-Xmx320m -Xss208m但它再次运行了一段时间并失败了。

My function prints recursively the entire tree of the schema definition,amazingly the output file crossed 820Mb for a definition file of 4 Mb(Aixm library) which in turn uses 50 Mb of schema definition library(ISO gml).

我的函数递归打印模式定义的整个树,令人惊讶的是,对于 4 Mb(Aixm 库)的定义文件,输出文件超过了 820Mb,而后者又使用了 50 Mb 的模式定义库(ISO gml)。

with that I am convinced I have to avoid Recursion and then start iteration and some other way of representing the output, but I am having little trouble converting all that recursion to iteration.

我确信我必须避免递归,然后开始迭代和其他一些表示输出的方式,但是我将所有递归转换为迭代几乎没有问题。

回答by ticu0000

When the argument -Xssdoesn't do the job try deleting the temporary files from:

当参数-Xss不起作用时,请尝试从以下位置删除临时文件:

c:\Users\{user}\AppData\Local\Temp\.

This did the trick for me.

这对我有用。

回答by Maria Sakharova

Look at Morris in-order tree traversal which uses constant space and runs in O(n) (up to 3 times longer than your normal recursive traversal - but you save hugely on space). If the nodes are modifiable, than you could save the calculated result of the sub-tree as you backtrack to its root (by writing directly to the Node).

查看使用恒定空间并以 O(n) 运行的 Morris 有序树遍历(最多比正常递归遍历长 3 倍 - 但您可以节省大量空间)。如果节点是可修改的,那么您可以在回溯到其根时保存子树的计算结果(通过直接写入节点)。