Java VM突然退出,没有明显的原因 [英] Java VM suddenly exiting without apparent reason

查看:123
本文介绍了Java VM突然退出,没有明显的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我的Java程序突然退出,没有引发任何异常或程序正常完成.

I have a problem with my Java progam suddenly exiting, without any exception thrown or the program finishing normally.

我正在编写一个程序来解决 Project Euler

I'm writing a program to solve Project Euler's 14th problem. This is what I got:

private static final int INITIAL_CACHE_SIZE = 30000;
private static Map<Long, Integer> cache = new HashMap<Long, Integer>(INITIAL_CACHE_SIZE);

public void main(String... args) {
    long number = 0;
    int maxSize = 0;

    for (long i = 1; i <= TARGET; i++) {
        int size = size(i);
        if (size > maxSize) {
            maxSize = size;
            number = i;
        }
    }
}
private static int size(long i) {
    if (i == 1L) {
        return 1;
    }
    final int size = size(process(i)) + 1;
    return size;
}

private static long process(long n) {
    return n % 2 == 0 ? n/2 : 3*n + 1;
}

运行正常,使用TARGET 1 000 000时,可以在约5秒钟内正确完成.

This runs fine, and finishes correctly in about 5 seconds when using a TARGET of 1 000 000.

我想通过添加缓存来进行优化,因此我将size方法更改为:

I wanted to optimize by adding a cache, so I changed the size method to this:

private static int size(long i) {
    if (i == 1L) {
        return 1;
    }
    if (cache.containsKey(i)) {
        return cache.get(i);
    }
    final int size = size(process(i)) + 1;
    cache.put(i, size);
    return size;
}

现在,当我运行它时,当我到达555144时,它只是停止(进程退出).每次都使用相同的数字.没有异常,错误,Java VM崩溃或引发任何异常.

Now when I run it, it simply stops (process exits) when I get to 555144. Same number every time. No exception, error, Java VM crash or anything is thrown.

更改缓存大小似乎也没有任何效果,所以缓存怎么可能 介绍会导致此错误?

Changing the cache size doesn't seem to have any effect either, so how could the cache introduction cause this error?

如果我强制将缓存大小不只是初始的,而是永久的,就像这样:

If I enforce the cache size to be not just initial, but permanent like so:

    if (i < CACHE_SIZE) {
        cache.put(i, size);
    }

该错误不再发生. 当我将缓存大小设置为2M时,该错误再次开始显示.

the bug no longer occurs. When I set the cache size to like 2M, the bug starts showing again.

任何人都可以复制它,甚至可以提供关于它为什么发生的建议吗?

Can anyone reproduce this, and maybe even provide a suggestion as to why it happens?

推荐答案

这只是一个OutOfMemoryError,未被打印.如果我设置了较大的堆大小,则程序可以正常运行,否则它会以未记录的OutOfMemoryError退出(尽管在调试器中很容易看到).

This is simply an OutOfMemoryError that is not being printed. The program runs fine if I set a high heap size, otherwise it exits with an unlogged OutOfMemoryError (easy to see in a Debugger, though).

您可以通过传递此JVM JVM参数并重新运行程序来验证这一点并获得堆转储(以及发生OutOfMemoryError的打印输出):

You can verify this and get a heap dump (as well as printout that an OutOfMemoryError occurred) by passing this JVM arg and re-running your program:

-XX:+ HeapDumpOnOutOfMemoryError

这样,它将打印出一些效果:

With this it will then print out something to this effect:

java.lang.OutOfMemoryError:Java堆空间
将堆转储到java_pid4192.hprof ...
创建的堆转储文件[在4.464秒内为91901809字节]

java.lang.OutOfMemoryError: Java heap space
Dumping heap to java_pid4192.hprof ...
Heap dump file created [91901809 bytes in 4.464 secs]

用-Xmx200m增大堆大小,不会有问题-至少对于TARGET = 1000000.

Bump up your heap size with, say, -Xmx200m and you won't have an issue - At least for TARGET=1000000.

这篇关于Java VM突然退出,没有明显的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆