3

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?

Abhij
  • 1,222
  • 4
  • 19
  • 37

2 Answers2

2

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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • `on Linux, Java appears to be limited to about 32K threads` even for 64-bit JVM? – Cratylus Dec 25 '12 at 18:33
  • I am not sure what the limit is but I have seen a 64-bit JVM fail to create more than about 32,000 threads on RHEL/Centos (on a machine with plenty of resources and a very high ulimit) – Peter Lawrey Dec 25 '12 at 20:11
0
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.
abishkar bhattarai
  • 7,371
  • 8
  • 49
  • 66