解决java.lang.StackOverflowError内存问题

发布于 2024-11-30 08:41:32 字数 885 浏览 6 评论 0原文

我正在使用递归函数,并且在执行时收到此错误:

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)

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

所以我认为这与记忆问题有关。

有什么解决办法吗?

编辑:

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);
   }
}

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

我正在做的是防止打开文件,如果以前是多普内。

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.

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 ...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑凤梨 2024-12-07 08:41:32

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

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

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


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

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

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

If not, then every time openFiles is 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.

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).


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 files argument passed in (I appreciate there is elided code, and presumably the contents get copied to _toOpen, but this seems unusual to say the least).

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.

清风无影 2024-12-07 08:41:32

我认为问题出在你的逻辑和 _toOpen.size() > 的测试中0 。

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


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

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

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

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


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

Your Vector _toOpen is static which means there is only one of it, which means that as soon as _toOpen.add(word); is triggered, the statement _toOpen.size() > 0 always holds true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文