Too many open files error and ulimit

I’ve been running some tests on my Mac where I was opening lots of connections from my virtual clients to my server and I suddenly ended up with this error:

java.io.IOException: Too many open files in system

As you guessed, there’s a limit to the number of open files in most of the operating systems. You can see the limit on your machine using ulimit -a. Note the open files limit is 256:

matamel-MacBookPro:etc matamel$ ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) 6144
file size (blocks, -f) unlimited
max locked memory (kbytes, -l) unlimited
max memory size (kbytes, -m) unlimited
open files (-n) 256
pipe size (512 bytes, -p) 1
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 266
virtual memory (kbytes, -v) unlimited

And, you can change the open files limit using ulimit -n. In this case, I increased my open files by 10:

matamel-MacBookPro:etc matamel$ ulimit -n 2560
matamel-MacBookPro:etc matamel$ ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) 6144
file size (blocks, -f) unlimited
max locked memory (kbytes, -l) unlimited
max memory size (kbytes, -m) unlimited
open files (-n) 2560
pipe size (512 bytes, -p) 1
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 266
virtual memory (kbytes, -v) unlimited

Note that this only applies to the shell you’re currently in although I’m sure there’s a way to make this permanent on Mac OS, just didn’t spend the time to look for it. I know that in Suse, you can make this change permanent by adding the following to /etc/security/limits.conf

* soft nofile 10240
* hard nofile 20480

In Windows, I seem to remember that this was controlled by a registry setting called MaxUserPort but might be wrong.

3 thoughts on “Too many open files error and ulimit

  1. I get the following (on Mac OS X):

    $ ulimit -n
    256

    $ ulimit -n 2048
    $ ulimit -n
    2048

    $ ulimit -n 4096
    -bash: ulimit: open files: cannot modify limit: Operation not permitted

    I appreciate that in Linux this can be overcome by editing /etc/security/limits.conf (as you say)

    How can I increase this limit in Mac OS X?

  2. On OS X:

    $ cd ~
    $ emacs .profile

    add “ulimit -a 1024” or whatever you think is adequate to the file
    then save (ctrl xc and answer “yes”)

    restart your terminal and check using
    $ ulimit -a

    to make sure it works

  3. Sorry – that should be

    $ cd ~
    $ emacs .profile

    add “ulimit -n 1024″ or whatever you think is adequate to the file
    then save (ctrl xc and answer “yes”)

    restart your terminal and check using
    $ ulimit -n

    to make sure it works

Leave a reply to Jalaluddin Morris Cancel reply