• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Max number of threads in a JVM

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
Would like to know what is maximum number of threads which can be created in a single virtual machine.
Information says that there is effect of settings of Xmx Xms and Xss on the number of threads created.
I see no difference when I execute attached code with different values.
Instead I observed that increasing values of -Xmx actually reduces the number of threads created, whereas Xss values do not have any effect.

I am using Windows 2000 , jdk1.5 , 1.5GB ram.

Any reasons for such behaviour.


public class ThreadTest {

public static void main(String[] pArgs) throws Exception {

try {
while (true) {
new TestThread().start();
}
} catch ( OutOfMemoryError e ) {
System.out.println(TestThread.CREATE_COUNT );
System.exit(-1);
}
}

static class TestThread extends Thread {
private static int CREATE_COUNT = 0;

public TestThread() {
CREATE_COUNT++;
}

public void run() {
try {
sleep(Integer.MAX_VALUE );
} catch (InterruptedException e) {
System.out.println("Interrupted");
}
}
}

}
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not aware of a strict maximum in threads but I do know that even if there was, getting close to it would almost certainly introduce a massive context switching overhead.

Are you trying to find out out of curiousity or are you developing an application where you are running into a thread limit? In the case of the latter you should really redesign that part of your application. If you give us a quick description of what you're trying to implement perhaps we can offer you an alternative.
 
chandrakant karale
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I apache tomcat , for one of the servlets I am getting out of memory error.
The servlet tries to connect to multiple databases in a separate instance of each thread.
We are stress testing the application where in the number of databases are increased gradually from 500 to 5000.
At near about 1200 databases, tomact throws out of memory error.

So wrote a test program (posted) to check what is the limit on number of threads which can be created inside a jvm.

One more finding - there is a difference in behavior between 1.5 and 1.6.
In 1.6 the -Xss parameter works, and the stack size per thread is taken according to the parameter.
But then there is no clear information about the expected behavior.
 
R van Vliet
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So to recap, you have a single servlet that spawns a thread for each new database (JDBC?) connection.

Is each thread connecting to a different database, or are they all connecting to one or a few specific databases? It's very hard for me to imagine a situation where a single application would want to connect to 1200 seperate databases. If it's only one logical database you really shouldnt start spawning thousands of threads. In fact, I'm not a huge fan of spawning new threads within a servlet doGet/doPost to begin with.

What is it you're trying to do in the servlet with all those databases exactly?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What message do you get with the OutOfMemoryError?
 
chandrakant karale
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its not a single logical database; every db is seperate.
Doing Stress testing to identify break point of application. But want to know what is limit or what limits creation of more number of threads.


Message from tomcat.log -

java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Thread.java:574)
 
R van Vliet
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hm, well, as I said, creating such a huge number of threads is relatively pointless to begin with for any commercial application due to the potentially high context switching overhead.

As for native thread limits and their associated memory usage, I would assume that that's at least in part OS specific and thus quite hard to determine.

I would seriously consider redesigning the app to use some sort of thread pooling and such. I can't think of a single scenario where not only you need a huge number of connection to seperate databases but you need a thread to manage each one as well. I've written quite a few application that coped with high concurrency problems such as these and there's almost always a solution that uses system resources less aggressively than this.

I'd be more than willing to have a look at it if you want.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by chandrakant karale:
But want to know what is limit or what limits creation of more number of threads.



That's the wrong question. The right question is: what is the best number of Threads. Actually nobody has an answer and will say: it depends on your application/envrionment.
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A servlet container maintains a thread pool and assigns a thread from the pool to service each request. You can create one or more custom thread pools to use instead of the default thread pool, but I have to really question the design of any application which needs concurrent access to 1,200 databases.
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by chandrakant karale:

java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Thread.java:574)



From the above error message, i do not think the maximum number of threads (if there is such a number) has reached. It just is an OutOfMemory error suggesting that the amount of memory JVM requires to spawn a new thread is not available. It has no association whatsoever with maximum number of allowed threads.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it really reasonable to expect the JVM that hosts the servlets to also handle this huge number of database connections? Seems to me we are getting into "grid computing" country here. How about a JavaSpaces server to coordinate multiple database workers?

Bill
 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would approach this in different way !. Have connection pools setup for all the database connections you want to connect and have the max and min connections as 1. and then use and release each connections as you progress
. This way each thread will not have to create individual connections ! especially these many numbers

Thanks
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic