如何增加最大 JVM 线程数(Linux 64 位)

2022-01-16 00:00:00 multithreading jvm linux java

我不能在 15G 内存的 Linux 机器上创建超过 32k 的 Java 线程.

I cannot create more than 32k Java threads in Linux machine with 15G memory.

推荐答案

你可以使用 示例程序来找出当前线程限制.

You can use a sample program to find out the current threads limit.

如果您遇到 Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread,请检查:

If you encounter Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread, check these:

  1. 在小型内存机器中

  1. In small memory machines

每个 Java 线程都消耗自己的堆栈内存.默认堆栈大小为 1024k (= 1M).您可以像 java -Xss512k ... 那样减小堆栈大小.如果堆栈太小,JVM 将无法启动.

Every Java thread consume its own stack memory. Default stack size is 1024k (= 1M). You can reduce the stack size like java -Xss512k .... JVM cannot be started if the stack size is too low.

注意堆内存配置:(初始)-Xms 和(最大)-Xmx.分配给堆的内存越多,堆栈的可用内存就越少.

And beware heap memory configurations: (initial) -Xms and (maximum) -Xmx. The more memory is allocated to heap, the less available memory for stack.

系统限制

ulimit -a 中的某些值会影响线程限制.

Some values in ulimit -a can affect a thread limit.

  • 最大内存大小 - 在大多数 64 位机器上无限制
  • max user processes - linux 将线程视为进程
  • 虚拟内存 - 在大多数 64 位机器上无限制.-Xss 配置增加了虚拟内存使用量(默认 1024k)
  • max memory size - unlimited on most 64bit machines
  • max user processes - linux treats threads like processes
  • virtual memory - unlimited on most 64bit machines. virtual memory usage is increased by -Xss configuration (default 1024k)

您可以通过(临时)运行 ulimit 命令或(永久)编辑 /etc/security/limits.conf 来更改这些值.

You can change these values by (temporal) running ulimit command or (permanent) editing /etc/security/limits.conf.

sys.kernel.threads-max

这个值是系统全局(包括非JVM进程)的最大线程数.检查cat/proc/sys/kernel/threads-max,必要时增加.

This value is the system-global (including non-JVM processes) maximum number of threads. Check cat /proc/sys/kernel/threads-max, and increase if necessary.

回显 999999 >/proc/sys/kernel/threads-max

/etc/sysctl.conf 中的 sys.kernel.threads-max = 999999 永久更改.

sys.kernel.pid_max

如果 cat/proc/sys/kernel/pid_max 类似于电流限制,则增加它.Linux 将线程视为进程.

If cat /proc/sys/kernel/pid_max is similar to current limit, increase this. Linux treats threads like processes.

回显 999999 >/proc/sys/kernel/pid_max

/etc/sysctl.conf 中的 sys.kernel.pid_max = 999999 永久更改.

您可能还需要增加 sys.vm.max_map_count.

sys.vm.max_map_count

cat/proc/sys/vm/max_map_count 至少应为 (2 x thread-count).

cat /proc/sys/vm/max_map_count should be at least (2 x thread-count).

尝试保护堆栈保护页面失败.OpenJDK 64 位服务器 VM 警告:尝试解除分配堆栈保护页面失败. JavaThread 发出错误消息::create_stack_guard_pages(),它调用 os::guard_memory().在 Linux 中,这个函数是 mprotect().

Attempt to protect stack guard pages failed. and OpenJDK 64-Bit Server VM warning: Attempt to deallocate stack guard pages failed. error messages are emitted by JavaThread::create_stack_guard_pages(), and it calls os::guard_memory(). In Linux, this function is mprotect().

回显 1999999 >/proc/sys/vm/max_map_count

/etc/sysctl.conf 中的 sys.vm.max_map_count = 1999999 永久更改.

相关文章