Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java very limited on max number of threads?

We have a small text box with 512Mb of ram. We wanted to see how many threads we can create in Java in this box. To our surprise, we can't create many. Essentially the minimum stack size you can set with -Xss is 64k. Simple math will tell you that 64*7000 will consume 430Mb so we were only able to get it up to around 7000 threads or so and then we encountered this error:

java.lang.OutOfMemoryError: unable to create new native thread. 

Is this the true limit with Java? Per 512Mb of ram we can only squeeze in 7k number of threads or so?

like image 967
erotsppa Avatar asked Jul 05 '10 16:07

erotsppa


2 Answers

Use asynchronous IO (java nio) and you'll don't need 7k threads to support 7k clients, a few threads for handling io (5?) will be enough.
Take a look at Netty ;)

One thread for each client is a really bad design.

like image 105
Tobias P. Avatar answered Nov 02 '22 05:11

Tobias P.


Once you create your 7k threads, you're not going to have any memory to do anything useful. Perhaps you should have a rethink about the design of your application?

Anyway, isn't 512Mb quite small? Perhaps you could provide a bit more information about your application or perhaps the domain?

like image 37
Noel M Avatar answered Nov 02 '22 07:11

Noel M