Java程序耗尽RAM而不是

时间:2018-04-19 08:40:17

标签: 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"

1 个答案:

答案 0 :(得分:0)

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

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

$ ulimit -a

根据man ulimit检查用户的当前限制。