Evade the macOS ‘Too Many Open Files’ Error by Pushing the Limits

too many files open

In macOS Sierra, as on all modern operating system, there are limits on how many files can be open at a time. There are also limitations on how many processes, or programs, can run simultaneously. Apple has set these limits very conservatively, but those limits can cause problems in the form of a “too many open files” error. Let’s take a look at how to fix that, if you dare.

too many files open
When you have too many files open, the only answer is to increase the limit (Image Credit: Pexels, modified by Jeff Butts)

Follow These Steps With Caution, and Only on macOS Sierra 10.12

When I first modified my system’s maximum file open and running process limit, it froze several processes (including Terminal itself, and the Finder). I couldn’t force-quit any apps, couldn’t get any response from right-clicks, and was forced to manually power my Mac Mini off and boot into Recovery Mode to undo the damage I had done. I briefly thought I’d have to restore from Time Machine, but thankfully was able to reverse the changes I had made without resorting to that method. So, let me repeat: follow these steps with caution, and only on macOS Sierra 10.12.

Why Limit My Computer?

Deep in the core of macOS, we’ll find these limitations. To be more precise, the limits are actually deep in the kernel, the master process that runs everything else. This limit exists on all computer systems, to prevent programs from running amok and draining all of your system memory (RAM).

Apple, catering primarily to casual consumer users, has set these limits very conservatively. By default, a user can only have 256 files open and can only have 709 processes running simultaneously. By way of comparison, an Ubuntu installation on my Mac Mini allows for more than 33,000 open files concurrently, and I could not find a limit on the number of simultaneous running processes.

Pushing the Limits

These limitations are fine for casual Mac users, but developers often run afoul of them. The result, quite often, is an error message: “Too many files open in system.”

You can see these limits for yourself. Note that you may need to have UNIX tools and/or Xcode installed for these tests to work. Assuming you have the following commands, you should be able to see the limitations for yourself.

From Applications/Utilities/Terminal, issue these commands:

sysctl kern.maxfiles
sysctl kern.maxfilesperproc

That will show you the system limits on open files and running processes. You’ll find the user limits by issuing this command in Terminal:

ulist -a

If you’ve run into the “Too many files open” error, let’s look at how to resolve it by increasing the maximum limits.

First, Disable System Integrity Protection (SIP)

Before you can change the limitations, you’ll need to disable SIP. SIP was introduced in in OS X El Capitan to prevent certain system-owned files and directories from being modified by processes that don’t have the right entitlements. Even the root user, or a user with root privileges (sudo), can’t modify these files without SIP being disabled. To disable this mechanism, you’ll need to reboot into Recovery Mode by restarting your Mac and holding down Command-R when you hear the startup chime, and until the Apple logo appears.

Once your Mac has booted into Recovery Mode, you should click Utilities from the top menu bar, then Terminal. In Terminal, issue the following command:

csrutil disable

Once that’s done, you can reboot your Mac normally and log in.

Configuring Your New File Limits

Once you’ve logged in, you should once again open Terminal. In Terminal, type the following command:

sudo nano /Library/LaunchDaemons/limit.maxfiles.plist

Copy and paste the following content into the text editor that’s opened in your Terminal window. Provide your user password, if prompted.

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" 
 "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0"> 
 <dict>
 <key>Label</key>
 <string>limit.maxfiles</string>
 <key>ProgramArguments</key>
 <array>
 <string>launchctl</string>
 <string>limit</string>
 <string>maxfiles</string>
 <string>64000</string>
 <string>524288</string>
 </array>
 <key>RunAtLoad</key>
 <true/>
 <key>ServiceIPC</key>
 <false/>
 </dict>
</plist>

Note that to do this, you likely won’t be able to use the keyboard shortcut. Instead, right-click inside the Terminal window and choose paste. Then, press Control-X to save and close the text editor.

You’ll notice two numbers there: 64000 and 524288. The first is a soft limit, at which point your Mac will start preparing to stop allowing new file opens but still let them go. When the second number is reached, that’s the hard limit – at that point, you’ll get the “Too many files open in system” error.

Up Next: Increasing the Processes Limit

8 thoughts on “Evade the macOS ‘Too Many Open Files’ Error by Pushing the Limits

  • Nice information, but it isn’t necessary to disable and reenable SIP, so you can avoid the multiple reboots, making this a lot less painful. You can create the plist in place by just doing:

    sudo vi /Library/LaunchDaemons/limit.maxfiles.plist

    This will make the file’s owner be root:wheel, as needed. You then invoke launchctl to load the new settings:

    sudo launchctl load -w /Library/LaunchDaemons/limit.maxfiles.plist

    You can confirm your hard maxfiles limit, before and after, as you noted:

    sysctl kern.maxfiles

    And you can confirm your soft maxfiles limit, before and after, with:

    launchctl limit maxfiles

  • Bonjour,

    Je possède un macbook de mi-2012, et je n’arrive pas à augmenter le nombre de fichiers.
    Lorsque je copie dans la zone de texte le texte suivant

    <! DOCTYPE plist PUBLIC “- // Apple // DTD PLIST 1.0 // EN” http://www.apple.com/DTDs/PropertyList-1.0.dtd

    Label </ key>
    limit.maxfiles </ string>
    ProgramArguments </ key>

    launchctl </ string>
    limite </ string>
    maxfiles </ string>
    64000 </ string>
    524288 </ string>
    </ Array>
    RunAtLoad </ key>

    ServiceIPC </ key>

    </ Dict>
    </ Plist>

    Quand je sauvegarde, je dois mettre quoi comme nom de sauvegarde?

  • One of the things you missed in this is that changing the limits to increase the number of open files can cause slowdowns without a reboot. Even turning SIP off and on, your dynamic reallocation of the uproc (user process; see the ps command) and file tables will cause a remapping of kernel memory. In the case of the Mach microkernel, the SIP process will have to reorganize and relink driver entry points in order to maintain the open file list.

    It is always better to decrease the number of open files than increase them. However, if you are running into open file limits, I would highly recommend that you avoid getting caught by reducing the number of open files… or figure out who is opening more files than necessary, such as a program that is opening multiple sockets to a single server. If you are the developer, you may want to consider a better implementation or, if you are using web-based services, might want to consider setting Keep-Alive to true and reusing the same open connection!

      1. However, there is a way to accomplish this without messing with SIP or rebooting, so I wonder how much of an issue this will be in that case.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.