3

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:

enter image description here

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"

15
  • 2
    Suggested duplicate: “java.lang.OutOfMemoryError : unable to create new native Thread” Commented Apr 19, 2018 at 8:43
  • @ErwinBolwidt: I think that question is likely very related, but both my development and my targeted platform is Windows, so, there might or might not be different techniques and/or solutions to the problem. Commented Apr 19, 2018 at 8:50
  • Maybe you have a high number of open streams/ connections .
    – Arnaud
    Commented Apr 19, 2018 at 9:39
  • How do you know okhttp3 didn't create 2700 threads? Have you made a thread dump?
    – rustyx
    Commented Apr 19, 2018 at 9:41
  • @rustyx: I don't really, but Java Visual VM is saying that Live Peak was 67 (its in the middle of the screenshot). Commented Apr 19, 2018 at 9:42

1 Answer 1

0

java.lang.OutOfMemoryError: unable to create new native thread can be caused by exhaustion of file descriptors or OS native threads.

If you are on Linux you can run

$ ulimit -a

to check what are the current limits for your user as per man ulimit.

5
  • Thank you for the answer @Karol. I'm running on Windows 10, so, no ulimit for me. I added some details about how many threads I have to the question. I'm not sure what the limits are for Windows. Commented Apr 19, 2018 at 8:49
  • @Timir: although I appreciate Karol's answer, his answer focuses on Linux while the problem I'm having is on Windows. I added more information to the question about how many threads I'm creating and it's not that many. Commented Apr 19, 2018 at 9:11
  • Don't raise ulimit to avoid the error. The thread limit is already huge. Fix the underlying issue instead.
    – rustyx
    Commented Apr 19, 2018 at 9:43
  • @rustyx: well, first, I'm using Windows and so do my customers, so, no ulimit; second, I can't go to each person that downloads my app and change the setting of their computer. I would agree though about not raising ulimit on a Linux server. Commented Apr 19, 2018 at 9:49
  • @pupeno although I'm not familiar with Windows this answer suggests that the total thread limit will depend on stack space. It's my understanding that all Windows processes will compete for this stack space, thus it might depend on what else is running on this machine. Commented Apr 19, 2018 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.