I'm running my application from within IntelliJ for this experiment and it's crashing with this error:
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)
or
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)
I fired up Java Visual VM and this is what I see:
The error happened around the vertical line. I'm puzzled by this because the used memory didn't get that close to the available memory and the available memory is only about half the max that's allowed. Also, during this exercise, my computer had more than 10GB of actual RAM free. This is running on a Windows 10 computer.
I created this little program to explore reaching the limit of threads:
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);
}
}
}
and even though it did crash with java.lang.OutOfMemoryError: unable to create new native thread
it took about 2700 threads to get there. I don't think my app gets anywhere near that number.
Any ideas what's going on here?
This may or may not be related: this seems to happen only with Java 32 bit and not 64 bit. What also is happen only with 32 bit is this: EXCEPTION_ACCESS_VIOLATION on "JavaFX Application Thread"