Java very limited on max number of threads?

10,662

Solution 1

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.

Solution 2

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?

Solution 3

It's not the programming language, it's on the operating system level.

More reading about it, for Windows:

Solution 4

Keep in mind that you will never be able to dedicate 100% of the RAM to running Java threads. Some RAM is used by the OS and other running applications, meaning you will never have the full 512 Mb available.

Solution 5

You don't necessarily need one thread per client session. If you look at the way that a J2EE (or JavaEE) server handles multiple connections it uses a mixture of strategies including concurrency, queuing and swapping. Usually you can configure the maximum number of live concurrent instances and idle time-out values at deployment time to tune the performance of your application.

Share:
10,662

Related videos on Youtube

erotsppa
Author by

erotsppa

Updated on April 26, 2022

Comments

  • erotsppa
    erotsppa about 2 years

    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?

    • Bwmat
      Bwmat almost 14 years
      do you really need that many threads? How many cpus/cores does the machine have?
    • Jason Coco
      Jason Coco almost 14 years
      If you require 7000 native threads, you have a serious design flaw in your application.
    • Gnoupi
      Gnoupi almost 14 years
      @erotsppa - and that's why you create connection pools.
    • Jason Coco
      Jason Coco almost 14 years
      @erotsppa: multiplex the io, don't just create unbounded threads or rethink your protocol design and prefer non-persisted connections.
  • Jason Coco
    Jason Coco almost 14 years
    In this person's case, they're hitting a memory limit before they ever hit the kernel limit.
  • Gnoupi
    Gnoupi almost 14 years
    @Jason - indeed, I missed the "OutOfMemoryError" on the way.
  • ColinD
    ColinD almost 14 years
    I believe "OutOfMemoryError: unable to create new native thread" can be caused by the OS's per-process thread limit, despite being an OutOfMemoryError. Not 100% sure on that though.
  • Jason Coco
    Jason Coco almost 14 years
    Actually @ConlinD is right, process limits show up as an out of memory error as well.
  • Daniel Gruszczyk
    Daniel Gruszczyk over 8 years
    Is there still a limit on the number of nio threads? I am dealing with a 3rd party library now that seems to be creating thousands of threads, it is using nio tho... Seems to cause the same error described by OP...