有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

jvm如何在退出MemoryError时终止java应用程序

目前,我们有一个java应用程序运行-XX:+ExitOnOutOfMemoryError-XX:OnOutOfMemoryError='kill -9 %p'jvm标志,以确保它在获得OOME时退出。它过去工作得很好,直到最近我们遇到了一个奇怪的错误java.lang.OutOfMemoryError: unable to create native thread: possibly out of memory or process/resource limits reached。在做了一些研究之后,人们似乎在谈论这个问题,但对我们的用例没有任何有用的解决方案

https://plumbr.io/outofmemoryerror/unable-to-create-new-native-thread

下面是我测试不同用例的应用程序

class Test {
    public static void main(String[] args)
            throws InterruptedException
    {
        Thread t = new Thread(() -> {
            while (true) {
                new Thread(() -> {
                    try {
                        Thread.currentThread().join();
                    }
                    catch (InterruptedException e) {
                    }
                }).start();
            }
        });

        t.start();
        Thread.currentThread().join();
    }
}

运行

javac Test.java
java -XX:MaxDirectMemorySize=10 -XX:+ExitOnOutOfMemoryError -XX:+UnlockDiagnosticVMOptions -XX:+CrashOnOutOfMemoryError

在不停止程序运行的情况下为我提供java.lang.OutOfMemoryError: unable to create native thread: possibly out of memory or process/resource limits reached。 一种解决方案是使用flag-XX:AbortVMOnException=java.lang.OutOfMemoryError,但这并不理想,因为即使我的应用程序正在使用依赖项库,悄悄地删除OOME并悄悄地忽略它,也会导致我的应用程序失败

我知道增加操作系统中的资源,正如abve链接中提到的,但我正在寻找一种解决方案,如果应用程序遇到这些类型的错误,它将终止应用程序


共 (0) 个答案