Java 程序用完 RAM 但不是真的

标签 java windows visualvm

我在 IntelliJ 中为这个实验运行我的应用程序,它因以下错误而崩溃:

java.lang.OutOfMemoryError: unable to create new native thread
    at java.lang.Thread.start0(Native Method)
    at java.lang.Thread.start(Thread.java:717)
    at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:957)
    at java.util.concurrent.ThreadPoolExecutor.processWorkerExit(ThreadPoolExecutor.java:1025)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

java.lang.OutOfMemoryError: unable to create new native thread
    at java.lang.Thread.start0(Native Method)
    at java.lang.Thread.start(Thread.java:717)
    at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:957)
    at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1378)
    at okhttp3.Dispatcher.enqueue(Dispatcher.java:130)
    at okhttp3.RealCall.enqueue(RealCall.java:100)
    at retrofit2.OkHttpCall.enqueue(OkHttpCall.java:107)
    at com.pubnub.api.endpoints.Endpoint.async(Endpoint.java:127)
    at com.pubnub.api.managers.SubscriptionManager.startSubscribeLoop(SubscriptionManager.java:275)
    at com.pubnub.api.managers.SubscriptionManager.access$400(SubscriptionManager.java:31)
    at com.pubnub.api.managers.SubscriptionManager$5.onResponse(SubscriptionManager.java:324)
    at com.pubnub.api.managers.SubscriptionManager$5.onResponse(SubscriptionManager.java:275)
    at com.pubnub.api.endpoints.Endpoint$1.onResponse(Endpoint.java:201)
    at retrofit2.OkHttpCall$1.callSuccess(OkHttpCall.java:138)
    at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:117)
    at okhttp3.RealCall$AsyncCall.execute(RealCall.java:153)
    at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

我启动了 Java Visual VM,这就是我所看到的:

enter image description here

错误发生在垂直线附近。我对此感到困惑,因为已用内存并没有接近可用内存,而且可用内存仅为允许的最大值的一半左右。此外,在本次练习中,我的计算机有超过 10GB 的实际可用 RAM。这是在 Windows 10 计算机上运行。

我创建了这个小程序来探索达到线程的极限:

public class ThreadBlower {
    public static void main(String[] args) {
        System.out.println("Creating threads until we crash.");
        for (int i = 0; ; i++) {
            Thread thread = new Thread(() -> { while (true) { } });
            thread.setName(String.format("Thread %d", i));
            thread.start();
            System.out.printf("Running %d threads.%n", i);
        }
    }
}

即使它确实因 java.lang.OutOfMemoryError: unable to create new native thread 而崩溃,它也需要大约 2700 个线程才能到达那里。我不认为我的应用会接近这个数字。

知道这里发生了什么吗?

这可能相关也可能不相关:这似乎只发生在 Java 32 位而非 64 位上。仅在 32 位上发生的情况是:EXCEPTION_ACCESS_VIOLATION on "JavaFX Application Thread"

最佳答案

java.lang.OutOfMemoryError: unable to create new native thread 可能是由文件描述符或操作系统 native 线程耗尽引起的。

如果你在 Linux 上,你可以运行

$ ulimit -a

根据 man ulimit 检查您的用户的当前限制是什么.

关于Java 程序用完 RAM 但不是真的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49916497/

相关文章:

php - 在 mod_auth_sspi_1.0.4-2.2.2 中禁用登录提示

java - VisualGC 中的 "Compile Time"是什么?

java.io.FileNotFoundException(访问被拒绝)

Windows Azure 虚拟机在扩展时访问网络速度很慢

java - 如何获取我的java应用程序正在使用的最大堆大小?

安装插件时 VisualVM 挂起

java - 在 android 中使用 SOAP 本地 Web 服务

java - 从线程中动态删除和添加 vaadin 中的组件

java - 日历,传递 ArrayList<HashMap,并迭代它

java - Log4j2 JSONLayout 给出错误