Skip to content

Maximum Number Of Threads (Linux/MacOS) + OutOfMemory unable to create new native thread

  • by

We create threads in application to improve performance. So, how many maximum threads can we create?? You might wondering we can create threads as much as memory is available in system. So If you try running below program, what is the output you are going to get??

package com.whiteboardtalks.threads;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class MaxThreadCount {

    public static void main(String[] args) throws InterruptedException {
        int i = 0;
        BlockingQueue<String> queue = new ArrayBlockingQueue<>(10);
        while(true) {
            new Thread(() -> {
                try {
                } catch (InterruptedException e) {


Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(

So, only 2029 threads in my application. I am running this program MacOS High Sierra 10 with 8 GB RAM. So, if you see memory footprint of this program using jvisualvm, it will be very less. So, what is limiting to create more threads??

In OS, we have system limits. If you run below command on linux,  you will get some number. That’s the limiting factor.

ulimit -u

In MacOS, we can check using below command.

sysctl kern.num_taskthreads

Changing threads limit in Linux: 

To change the “soft” and “hard” limits for the maximum number of processes (threads) for the admin user, add the following lines to the /etc/security/limits.conf file:

admin         soft         nproc      4096

admin         hard       nproc      16384


If you want to change for all user, use * in place of username.

*         soft         nproc      4096

*         hard       nproc      16384

Save file and restart the process or terminal.

Soft Limit vs Hard Limit

  • A hard limit is the maximum allowed to a user, set by the root. This value is set in the file /etc/security/limits.conf.
  • soft limit is the value right now for that user. The user can increase the soft limit on their own in times of needing more resources, but cannot set the soft limit higher than the hard limit.

Leave a Reply

Your email address will not be published. Required fields are marked *