eclipse 解决 java.lang.StackOverflowError 内存问题

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

Resolve java.lang.StackOverflowError memory problem

javaeclipsememoryrecursionstack-overflow

提问by Wassim AZIRAR

I'm using a recursive function, and I'm getting this error when I execute :

我正在使用递归函数,执行时出现此错误:

Exception in thread "main" java.lang.StackOverflowError
    at java.util.HashMap$Entry.<init>(Unknown Source)
    at java.util.HashMap.addEntry(Unknown Source)
    at java.util.HashMap.put(Unknown Source)

I debeugged the method and 100% sure it ends at some point.

我调试了该方法,并且 100% 确定它会在某个时候结束。

So I think its related to a memory problem.

所以我认为它与内存问题有关。

Is there any solution ?

有什么解决办法吗?

EDIT:

编辑:

public static Vector<String> _toOpen;

public static void openFiles(Vector<String> files)
{       
   ...

    while(actualFile.hasNext)
    {
        if(!_toOpen.contains(word))
        {
           _toOpen.add(word);
            System.out.println("word");
        }
    }

   ...

   if(_toOpen.size() > 0)
   {
       openFiles(_toOpen);
   }
}

At the first call I pass to OpenFiles a Vector wich contains a list of files to open, each file has a list of files to open again and so on ...

在第一次调用时,我传递给 OpenFiles 一个 Vector,其中包含要打开的文件列表,每个文件都有一个要再次打开的文件列表,依此类推...

What I'm doing is preventing a file to be opened if it was dopne before.

我正在做的是阻止打开一个文件,如果它以前是dopne。

回答by Andrzej Doyle

Looking at your code - is there any conditional (e.g. iffor example) on your final call to openFiles(_toOpen)?

查看您的代码 - 您if对 的最终调用是否有任何条件(例如)openFiles(_toOpen)

If not, then every time openFilesis called, it will call itself recursively. No matter what the maximum size of the stack, this method will never actually return and you've effectively got an infinite loop.

如果不是,那么每次openFiles被调用时,它都会递归地调用自己。无论堆栈的最大大小是多少,此方法实际上永远不会返回,并且您实际上已经获得了无限循环。

And if there is some conditional beforehand, you're evidently getting into a situation where it's consistently evaluating to a true(or whatever leads to the recursive call being made).

如果事先有一些条件,您显然会陷入一种情况,即它一直在评估 a true(或任何导致进行递归调用的情况)。



Asides from that, it looks like you could restructure your code to avoid this. What it is you're trying to do with _toOpen? Why do you appear to ignore the filesargument passed in (I appreciate there is elided code, and presumably the contents get copied to _toOpen, but this seems unusual to say the least).

除此之外,您似乎可以重构代码以避免这种情况。你想做_toOpen什么?为什么你似乎忽略了files传入的参数(我很欣赏有省略的代码,大概内容被复制到_toOpen,但这至少可以说是不寻常的)。

A recursive call does not seem like it's the best way to approach this problem, unless you have some strange situation such as files that refer to other files to open.

递归调用似乎不是解决此问题的最佳方法,除非您遇到一些奇怪的情况,例如引用要打开的其他文件的文件。

回答by Charles Goodwin

I think the problem is in your logic and the test for _toOpen.size() > 0.

我认为问题出在你的逻辑和对_toOpen.size() > 0.

After you perform _toOpen.add(word);then _toOpen.size()will alwaysbe greater than 0, and thus the ifcondition always true, and the function will always recurse.

执行后_toOpen.add(word);then_toOpen.size()始终大于 0,因此if条件始终为真,并且函数将始终递归。



You say "But the _toOpen.add(word);is not triggered always" - but it has to be triggered only oncein the application life cycle to make this method recurse indefinitely.

您说“但_toOpen.add(word);并不总是触发” - 但它必须在应用程序生命周期中触发一次才能使此方法无限递归。

Your Vector _toOpen is staticwhich means there is only oneof it, which means that as soon as _toOpen.add(word);is triggered, the statement _toOpen.size() > 0alwaysholds true.

你的 Vector _toOpen 是静态的,这意味着它只有一个,这意味着一旦_toOpen.add(word);被触发,该语句_toOpen.size() > 0总是成立。