faq On Linux how do I fix java.lang.OutOfMemoryError unable to create new native Thread - padogrid/padogrid GitHub Wiki

On Linux, how do I fix "java.lang.OutOfMemoryError : unable to create new native Thread"?

This error occurs when your Java app is trying to creating more threads than it is allowed by the OS. In Linux, both a process and a thread are handled in the same way such that limits on processes indirectly reflect limits on threads.

Kernel Parameters

Take a look at the following kernel parameters. You will need to increase their values based on your requirements.

cat /proc/sys/kernel/threads-max
cat /proc/sys/kernel/pid_max
cat /proc/sys/vm/max_map_count

The following examples show how to set their values.

# Set temporarily - max of 100k threads executable by kernel
echo 100000 > /proc/sys/kernel/threads-max

# Set permanently - max of 100k threads executable by kernel
sysctl -w kernel.threads-max=100000 >> /etc/sysctl.conf
# Max of 400k processes simutaneously executable by kernel
echo 400000 > /proc/sys/kernel/pid_max

# Max of 500k Virtual Memory Areas (VMAs) owned by a process
echo 500000 > /proc/sys/vm/max_map_count

User Limits

Add user limits in /etc/security/limits.conf. The following example sets both hard (absolute) and soft (warning) limits for the number of processes and number of open files for the user, foo.

vi /etc/security/limits.conf
foo hard nproc 50000
foo soft nproc 50000
foo hard nofile 60000
foo soft nofile 60000

You can use ulimit to view the user limits.

# View all user limits
ulimit -a

# View user processes
ulimit -u

systemd

Finally, if your Java app is launched by systemd, then check to see if UserTasksMax is set to a low value. It specifes the TasksMax setting for all users and determines the thread limit.

Set or comment out UserTasksMask in /etc/systemd/system.conf:

vi /etc/systemd/system.conf
...
UserTasksMax=60000
...