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

  1. VirtualBox 7 + debian-12.7.0-amd64-DVD-1.iso
  2. 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

  1. 1CPU 1GB

    threads: 2400, memory usage: 51.8%

  2. 1CPU 2GB

    threads: 5057, memory usage: 31.2%

  3. 1CPU 4GB

    threads: 9985, memory usage: 22.7%

  4. 1CPU 8GB

    threads: 9985, memory usage: 12%

  5. 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:

  1. Xms, Xmx, Xss
  2. ulimit -u 100000
  3. /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

  1. VirtualBox 7 + debian-12.7.0-amd64-DVD-1.iso
  2. 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

  1. 1CPU 1GB

    threads: 2400, memory usage: 51.8%

  2. 1CPU 2GB

    threads: 5057, memory usage: 31.2%

  3. 1CPU 4GB

    threads: 9985, memory usage: 22.7%

  4. 1CPU 8GB

    threads: 9985, memory usage: 12%

  5. 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:

  1. Xms, Xmx, Xss
  2. ulimit -u 100000
  3. /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