无法追踪堆栈溢出错误的来源

发布于 2024-12-10 11:18:36 字数 921 浏览 0 评论 0原文

我有一个作为服务运行的 Java 应用程序。运行几天后,我收到堆栈溢出错误。问题是我的源代码中产生错误的堆栈帧不在 printStackTrace 函数报告的错误中。我不知道如何找到问题所在。

这是 printStackTrace 的结果:

java.lang.StackOverflowError
    at java.util.LinkedList.listIterator(LinkedList.java:684)
    at java.util.SubList$1.<init>(AbstractList.java:700)
    at java.util.SubList.listIterator(AbstractList.java:699)
    at java.util.SubList$1.<init>(AbstractList.java:700)
    at java.util.SubList.listIterator(AbstractList.java:699)
    at java.util.SubList$1.<init>(AbstractList.java:700)
    at java.util.SubList.listIterator(AbstractList.java:699)
        ... (this is repeated hundreds of times)
    at java.util.SubList.listIterator(AbstractList.java:699)
    at java.util.SubList$1.<init>(AbstractList.java:700)
    at java.util.SubList.listIterator(AbstractList.java:699)
    at java.util.SubList$1.<init>(AbstractList.java:700)

I have a Java application that is working as a service. After several days running I get a stack overflow error. The problem is that the stack frame in my source where the error is being originated is not in the error reported by printStackTrace function. I have no idea about how to find where the problem is.

This is the result of printStackTrace:

java.lang.StackOverflowError
    at java.util.LinkedList.listIterator(LinkedList.java:684)
    at java.util.SubList$1.<init>(AbstractList.java:700)
    at java.util.SubList.listIterator(AbstractList.java:699)
    at java.util.SubList$1.<init>(AbstractList.java:700)
    at java.util.SubList.listIterator(AbstractList.java:699)
    at java.util.SubList$1.<init>(AbstractList.java:700)
    at java.util.SubList.listIterator(AbstractList.java:699)
        ... (this is repeated hundreds of times)
    at java.util.SubList.listIterator(AbstractList.java:699)
    at java.util.SubList$1.<init>(AbstractList.java:700)
    at java.util.SubList.listIterator(AbstractList.java:699)
    at java.util.SubList$1.<init>(AbstractList.java:700)

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

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

发布评论

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

评论(2

吾性傲以野 2024-12-17 11:18:36

您的问题可以重现如下:

List<String> l = ...;

for (int i = 0; i < 2800; i++) {
    l = l.subList(...);
}
l.listIterator(...);

因此,请注意 subList() 的所有调用,并确保它们不会形成如上所述的长子列表链。

这样的链是一个递归结构(每个子列表都保留对其父列表的引用),在很长的链上调用listIterator()会导致非常深的递归,从而导致堆栈溢出。

Your problem can be reproduced as follows:

List<String> l = ...;

for (int i = 0; i < 2800; i++) {
    l = l.subList(...);
}
l.listIterator(...);

So, pay attention on all invocations of subList() and make sure they don't form a long chain of sublists as above.

Such a chain is a recursive structure (each sublist keeps a reference to its parent list), and calling listIterator() on a very long chain causes a very deep recursion which causes stack overflow.

智商已欠费 2024-12-17 11:18:36

查找 List.subList() 方法的使用 - 这是您可以获得 java.util.SubList 实例(具有包可见性的类)的唯一位置。

您还可以尝试更新到较新的 Java 版本 - 您的堆栈跟踪与当前版本中列表类的结构不匹配,因此您的问题可能不会在更新的版本上发生(或导致更容易诊断的不同错误)版本。

Look for uses of the List.subList() method - that's the only place where you can get an instance of java.util.SubList (a class with package visibility).

You could also try upading to a newer Java version - your stack trace doesn't match the structure of the list classes in current versions, so your problem may not occur (or result in a differen error that's easier to diagnose) on a more current version.

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