Where have all the threads gone?

Posted by Mike Haller on Monday, July 6. 2009 at 15:37 in Java
Recently, we got a nasty error in the log file:
java.lang.OutOfMemoryError: unable to create new native thread


Although we had specified -XX:HeapDumpOnOutOfMemoryError, this does not include dumping the heap to disk when the heap is not involved in an OutOfMemoryError. In this case, the JVM was unable to create new threads as the limit of threads was reached.

So, the question is: How many threads can be created?
The answer is, as usual: It depends!

It depends on the amount of memory of the operating system, the architecture of the platform and the size of each thread and how much memory it takes in the stack.

To show you how it depends on the total memory assigned to the JVM and that it's an inversely proportional function, i ran a tiny test. The test created Threads with an unlimited Thread.sleep(Integer.MAX_VALUE) and had no additional data, so the overhead is minimal/default. I ran the test on my business notebook, which has (according to Windows Task-Manager) 3.66GB of physical memory. The java version is Java 32Bit 1.6.0_13, client vm, mixed mode, sharing. The operating system is Windows XP 32-Bit.

The results show a very unintuitive, but logical behaviour: The more memory is available to Java, the less is available for OS Threads and thus the total number of threads which can be created decreases:

Created 5605 threads with avail. memory 63MB
Created 5478 threads with avail. memory 127MB
Created 5096 threads with avail. memory 254MB
Created 4285 threads with avail. memory 508MB
Created 2661 threads with avail. memory 1016MB
Created 1201 threads with avail. memory 1488MB
Created 909 threads with avail. memory 1587MB
Created 890 threads with avail. memory 1593MB


What can you do about it?
Well, if your bound to hardware resources, decrease the amount of memory available to the JVM or inspect the software components that they're releasing threads properly. Monitor your application with a JMX Monitor such as JConsole or the wonderful VisualVM.

Links:
How to fix "java.lang.OutOfMemoryError: unable to create new native thread"
IBM: Thanks for the memory
The magic /3GB switch of IBM JVMs

WHERE HAVE ALL THE THREADS GONE 
words and music by Pete Seeger
performed by Pete Seeger and Tao Rodriguez-Seeger
modified by mike

Where have all the threads gone?
Long time passing
Where have all the threads gone?
Long time ago
Where have all the threads gone?
Connections have picked them every one
When will they ever learn?
When will they ever learn?

Where have all the native threads gone?
Long time passing
Where have all the native threads gone?
Long time ago
Where have all the native threads gone?
Taken by processes every one
When will they ever learn?
When will they ever learn?

Where have all the java threads gone?
Long time passing
Where have all the java threads gone?
Long time ago
Where have all the java threads gone?
Gone for locking every one
When will they ever learn?
When will they ever learn?

Where have all the batch jobs gone?
Long time passing
Where have all the batch jobs gone?
Long time ago
Where have all the batch jobs gone?
Gone to deadlock every one
When will they ever learn?
When will they ever learn?

Where has all the memory gone?
Long time passing
Where has all the memory gone?
Long time ago
Where has all the memory gone?
Core dumped to disk every one
When will we ever learn?
When will we ever learn?




Add Comment

Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications
 
Submitted comments will be subject to moderation before being displayed.
 

About

My name is Mike Haller and I'm a software developer and architect at Bosch Software Innovations in Germany. I love programming, playing games and reading books. I like good food, making photos and learning and mentoring about the craftsmanship of commercial software development. Stack Overflow profile for mhaller

Quicksearch