aspose file tools*
The moose likes Performance and the fly likes Max number of threads in a JVM Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "Max number of threads in a JVM" Watch "Max number of threads in a JVM" New topic
Author

Max number of threads in a JVM

chandrakant karale
Ranch Hand

Joined: Nov 21, 2007
Posts: 41
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");
}
}
}

}
R van Vliet
Ranch Hand

Joined: Nov 10, 2007
Posts: 144
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

Joined: Nov 21, 2007
Posts: 41
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

Joined: Nov 10, 2007
Posts: 144
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?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
What message do you get with the OutOfMemoryError?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
chandrakant karale
Ranch Hand

Joined: Nov 21, 2007
Posts: 41
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

Joined: Nov 10, 2007
Posts: 144
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.
Walter Bernstein
Ranch Hand

Joined: Dec 19, 2007
Posts: 57
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.
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
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.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

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.


apigee, a better way to API!
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
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
Raees Uzhunnan
Ranch Hand

Joined: Aug 15, 2002
Posts: 126
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


Sun Certified Enterprise Architect
Java Technology Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Max number of threads in a JVM