The
following question is quite common and is related to OutOfMemoryError: unable to create new native thread problems during
the JVM thread creation process and the JVM thread capacity.
This is also a typical interview question I ask to new technical candidates (senior role). I recommend that you attempt to provide your own response before looking at the answer.
This is also a typical interview question I ask to new technical candidates (senior role). I recommend that you attempt to provide your own response before looking at the answer.
Question:
Why can’t
you increase the JVM thread capacity (total # of threads) by expanding the Java heap space capacity via -Xmx?
Answer:
The Java
thread creation process requires native memory to be available for the JVM
process. Expanding the Java heap space via the –Xmx argument will actually reduce
your Java thread capacity since this memory will be “stolen” from the native
memory space.
- For a 32-bit JVM, the Java heap space is in a race with
the native heap, including the thread capacity
- For a 64-bit JVM, the thread capacity will mainly depend
of your OS physical & virtual memory availability along with your
current OS process related tuning parameters
In order to better understand this limitation, I now propose to you the following video tutorial.
You can also download the sample Java program from the link below:
https://docs.google.com/file/d/0B6UjfNcYT7yGazg5aWxCWGtvbm8/edit
You can also download the sample Java program from the link below:
https://docs.google.com/file/d/0B6UjfNcYT7yGazg5aWxCWGtvbm8/edit
12 comments:
very good explanation. keepup the good work Pierre.
Thanks Shan for your comments and support.
Regards,
P-H
There was typo error in my previous comment
This is brilliant! Good job Pierre! My only confusion was about the exception you've got while trying to create java threads: The exception states: Unable to create new native thread although you were trying to create java threads. Does it mean that the java threads are created using the native space of the JVM?
Hi Anonymous and thanks for your positive comments,
Yes, you understood properly. Each time the JVM create a "Java Thread", this process also needs to allocate memory for the "native" thread creation. Some memory is allocated on the Java Heap but only the Java Thread representation/Obect instance.
The native thread creation process basically involving the JVM native code to request a new thread from the OS. This procees requires native memory available from the Java process memory space.
This is why that even if you have 1 GB of free Java Heap, you can still get this OOM event when running out of memory from the native memory address space.
Regards,
P-H
very good tutorial..We often run into production with oom errors
Suggestion: Can you also post a similar tutorial highliting the difference between permgen and heap memory.
Maybe how to find that sweet spot where you allocate just enough for both
Guy, you are giving really tough time to the java creators themselves. Very good explanation to the hardest problems of java. Keep it up..
Thanks.
Thanks for your comments,
Glad that you liked the tutorial and video.
This is indeed one of the hardest problems you can face with Java & the JVM.
Regards,
P-H
Thanks Pierre. It really helped. I was struck with this using ActiveMQ.
Thanks Bipin, glad this article was helpful to you.
Thanks Pierre
your article help me to get rid of a OOM error on which I was stuck for so long.
Thanks
Raj
rajkrrsingh.blogspot.com
Thanks Rajkumar,
I'm glad this article could help.
Regards,
P-H
Amazing tutorial. I faced the exact problem 3 years ago and it took me time to figure it out. Keep up the good work.
Post a Comment