Reputation: 1272
I have created a program to check max no of thread in java
public class Test extends Thread {
static int count;
public static void main (String [] args){
for(;;){
count++;
System.out.println(count);
new Test().start();
}
}
@Override
public void run() {
try {
Thread.sleep(100000000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
First iteration -Xmx1024m ,max thread = 2011 > Second iteration -Xmx512m ,max thread = 3350 > third iteration -Xmx2m ,max thread = 5112
I have also tried with setting -Xss1m ,max thread = 1011, then I have set -Xss256k max thread 4900+
I have two questions
1)what is relation of stack and heap size in java?
2)On what factor does max no of thread depends in java?
Upvotes: 3
Views: 575
Reputation: 7641
May be the answer is in
java.lang.Runtime class
java.lang.management package
java.lang.instrument package
If you can see it in depth.
Upvotes: 0
Reputation: 533530
If you are running on a 32-bit VM you have a limited virtual memory space. On the 32-bit windows JVM it can be as small as 1.5 GB. Each thread needs stack space so the more free space you have after the heap the more threads you can have. On 32-bit Unix, you can hae up to 3.5 GB of virtual memory, but the limit is still there.
If you are running a 64-bit JVM, this limit is lifted and you can have a very large heap e.g. 1 TB and still not restrict the number for threads you can have. Note: on Linux, Java appears to be limited to about 32K threads. I don't suggest you use anything like this number in any case, You should try to keep it to less than a few thousand.
Upvotes: 2