admin管理员组文章数量:1022727
foreword
I want to find out the maximum number of threads supported by the jvm under the default configuration, and figure out what configuration changes can increase this number.
environment
- VirtualBox 7 + debian-12.7.0-amd64-DVD-1.iso
- Java 21 + SpringBoot3
koril@TestDebian:~/project$ uname -a
Linux TestDebian 6.1.0-27-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.115-1 (2024-11-01) x86_64 GNU/Linux
koril@TestDebian:~/project$ java -version
java version "21.0.5" 2024-10-15 LTS Java(TM) SE Runtime Environment (build 21.0.5+9-LTS-239) Java HotSpot(TM) 64-Bit Server VM (build 21.0.5+9-LTS-239, mixed mode, sharing)
my code
controller:
@RestController
@RequestMapping("/thread")
public class ThreadController {
@GetMapping("/add")
public String add(int num) {
for (int i = 0; i < num; i++) {
new Thread(new TestTask()).start();
}
return "Add " + num + " threads success";
}
}
TestTask.java:
public class TestTask implements Runnable {
@Override
public void run() {
while (true) {
try {
Thread.sleep(Long.MAX_VALUE);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
my test
I use jconsole to see the number of jvm threads and Linux top command to see the memory usage.
This is start jar script:
#!/bin/bash
java \
-Djava.rmi.server.hostname=192.168.0.202 \
-Dcom.sun.management.jmxremote \
-Dcom.sun.management.jmxremote.port=12345 \
-Dcom.sun.management.jmxremote.ssl=false \
-Dcom.sun.management.jmxremote.authenticate=false \
-jar study-java-thread.jar
I logged the number of threads in jconsole when it gave the following error:
java.lang.OutOfMemoryError: unable to create native thread: possibly out of memory or process/resource limits reached
test results
1CPU 1GB
threads: 2400, memory usage: 51.8%
1CPU 2GB
threads: 5057, memory usage: 31.2%
1CPU 4GB
threads: 9985, memory usage: 22.7%
1CPU 8GB
threads: 9985, memory usage: 12%
1CPU 16GB
threads: 9985, memory usage: 6.3%
confusion
Why does the number always stay at 9985 as the memory of the virtual machine increases?
I modified the following options, but it seems to have no real effect:
- Xms, Xmx, Xss
- ulimit -u 100000
- /proc/sys/kernel/threads-max
Is there a formula that can calculate the thread limit?
foreword
I want to find out the maximum number of threads supported by the jvm under the default configuration, and figure out what configuration changes can increase this number.
environment
- VirtualBox 7 + debian-12.7.0-amd64-DVD-1.iso
- Java 21 + SpringBoot3
koril@TestDebian:~/project$ uname -a
Linux TestDebian 6.1.0-27-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.115-1 (2024-11-01) x86_64 GNU/Linux
koril@TestDebian:~/project$ java -version
java version "21.0.5" 2024-10-15 LTS Java(TM) SE Runtime Environment (build 21.0.5+9-LTS-239) Java HotSpot(TM) 64-Bit Server VM (build 21.0.5+9-LTS-239, mixed mode, sharing)
my code
controller:
@RestController
@RequestMapping("/thread")
public class ThreadController {
@GetMapping("/add")
public String add(int num) {
for (int i = 0; i < num; i++) {
new Thread(new TestTask()).start();
}
return "Add " + num + " threads success";
}
}
TestTask.java:
public class TestTask implements Runnable {
@Override
public void run() {
while (true) {
try {
Thread.sleep(Long.MAX_VALUE);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
my test
I use jconsole to see the number of jvm threads and Linux top command to see the memory usage.
This is start jar script:
#!/bin/bash
java \
-Djava.rmi.server.hostname=192.168.0.202 \
-Dcom.sun.management.jmxremote \
-Dcom.sun.management.jmxremote.port=12345 \
-Dcom.sun.management.jmxremote.ssl=false \
-Dcom.sun.management.jmxremote.authenticate=false \
-jar study-java-thread.jar
I logged the number of threads in jconsole when it gave the following error:
java.lang.OutOfMemoryError: unable to create native thread: possibly out of memory or process/resource limits reached
test results
1CPU 1GB
threads: 2400, memory usage: 51.8%
1CPU 2GB
threads: 5057, memory usage: 31.2%
1CPU 4GB
threads: 9985, memory usage: 22.7%
1CPU 8GB
threads: 9985, memory usage: 12%
1CPU 16GB
threads: 9985, memory usage: 6.3%
confusion
Why does the number always stay at 9985 as the memory of the virtual machine increases?
I modified the following options, but it seems to have no real effect:
- Xms, Xmx, Xss
- ulimit -u 100000
- /proc/sys/kernel/threads-max
Is there a formula that can calculate the thread limit?
本文标签: linuxConfusion about the maximum number of Java threads under Debian12Stack Overflow
版权声明:本文标题:linux - Confusion about the maximum number of Java threads under Debian12 - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745572018a2156790.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论