I've checked these two posts OSX 10.6.3 and JVM support about threads limit in StackOverflow, but when I tried to test my system thread count limit using the following test, I will always get far less (like about 2000
) than they mentioned in these two posts (like 40_000
).
I also check this less is more about thread limit to make more local tests, but the results are still very frustrating.
F.Y.I
Using sysctl kern.num_threads
to check thread limit in my mac and I got kern.num_threads: 10240
-Xmx2048m -Xms2048m -Xss200k ==> 1286
-Xmx4096m -Xms3072m -Xss320k ==> 1048
-Xmx4096m -Xms3072m ==> 1153
-Xmx32m -Xms32m ==> 1309
-Xmx32m ==> 2027
Is there something I missed to result in this bad performance (low thread limit)?
public class TestThreadCountLimit {
private static Object s = new Object();
private static int count = 0;
public static void main(String[] argv) {
for (; ; ) {
Thread thread = new Thread(() -> {
synchronized (s) {
count += 1;
System.err.println("New thread #" + count);
}
for (; ; ) {
try {
Thread.sleep(1000);
} catch (Exception e) {
System.err.println(e);
}
}
});
thread.setDaemon(true); // to ensure I can terminate them, I set them daemon on purpose;
thread.start();
}
}
}