macOS: Increase Open Files
Understanding Low File Descriptors on macOS Link to heading
When working on a macOS system, you might notice that the number of open file descriptors per process is relatively low by default. This can cause issues for applications that require handling many files or network connections simultaneously, such as servers or development tools. In this blog post, we will explore what file descriptors are, why macOS sets a low default limit, and how to address it.
What Are File Descriptors? Link to heading
File descriptors are identifiers used by a process to reference open files, sockets, pipes, or other I/O resources. Each process on a Unix-like system, including macOS, has a limited number of file descriptors it can use. When this limit is reached, attempts to open new files or connections will fail, often resulting in errors like Too many open files.
To check the current limits on your macOS system, you can run the ulimit -a
command in the Terminal. Here is an example output of ulimit -a
:
$ ulimit -a
-t: cpu time (seconds) unlimited
-f: file size (blocks) unlimited
-d: data seg size (kbytes) unlimited
-s: stack size (kbytes) 8176
-c: core file size (blocks) 0
-v: address space (kbytes) unlimited
-l: locked-in-memory size (kbytes) unlimited
-u: processes 10666
-n: file descriptors 256
In this output, the -n
flag shows the maximum number of open file descriptors per process, which is set to 256
. This is the default on many macOS systems and is relatively low compared to other Unix-like systems, where limits might be 1024 or higher.
Checking the Current Limit Link to heading
As shown above, you can check the current file descriptor limit with ulimit -n
. The output of ulimit -a
provides a broader view of resource limits, including the file descriptor limit (-n: file descriptors
).
Temporarily Increasing the Limit Link to heading
To increase the file descriptor limit for the current Terminal session, you can use the ulimit
command. For example:
ulimit -n 1024
This sets the file descriptor limit to 1024 for the current session only. However, this change is not persistent and will reset when you close the Terminal or start a new session.
What did I do? Link to heading
I put ulimit -n 4096
at the end of ~/.zshrc
, which is for the z shell that I am using. This will make the change persistent.