Skip to content

Instantly share code, notes, and snippets.

@devinrhode2
Forked from tombigel/README.md
Last active August 6, 2024 10:30
Show Gist options
  • Save devinrhode2/4cbf7f02a9701510d61f5be0515b8286 to your computer and use it in GitHub Desktop.
Save devinrhode2/4cbf7f02a9701510d61f5be0515b8286 to your computer and use it in GitHub Desktop.
How to Change Open Files Limit on OS X and macOS Sierra (10.8 - 10.12)

How to Change Open Files Limit on OS X and macOS

This text is the section about OS X Yosemite (which also works for macOS Sierra) from https://docs.basho.com/riak/kv/2.1.4/using/performance/open-files-limit/#mac-os-x

The last time i visited this link it was dead (403), so I cloned it here from the latest snapshot in Archive.org's Wayback Machine https://web.archive.org/web/20170523131633/https://docs.basho.com/riak/kv/2.1.4/using/performance/open-files-limit/

Mac OS X

To check the current limits on your Mac OS X system, run:

launchctl limit maxfiles

The last two columns are the soft and hard limits, respectively.

Adjusting Open File Limits in Yosemite

To adjust open files limits on a system-wide basis in Mac OS X Yosemite, you must create two configuration files. The first is a property list (aka plist) file in /Library/LaunchDaemons/limit.maxfiles.plist that contains the following XML configuration:

<?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>200000</string>
          <string>200000</string>
        </array>
      <key>RunAtLoad</key>
        <true/>
      <key>ServiceIPC</key>
        <false/>
    </dict>
  </plist>

This will set the open files limit to 200000. The second plist configuration file should be stored in /Library/LaunchDaemons/limit.maxproc.plist with the following contents:

<?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.maxproc</string>
      <key>ProgramArguments</key>
        <array>
          <string>launchctl</string>
          <string>limit</string>
          <string>maxproc</string>
          <string>2048</string>
          <string>2048</string>
        </array>
      <key>RunAtLoad</key>
        <true />
      <key>ServiceIPC</key>
        <false />
    </dict>
  </plist>

Both plist files must be owned by root:wheel and have permissions -rw-r--r--. This permissions should be in place by default, but you can ensure that they are in place by running sudo chmod 644 . While the steps explained above will cause system-wide open file limits to be correctly set upon restart, you can apply them manually by running launchctl limit.

In addition to setting these limits at the system level, we recommend setting the at the session level as well by appending the following lines to your bashrc, bashprofile, or analogous file:

ulimit -n 200000
ulimit -u 2048

Like the plist files, your bashrc or similar file should have -rw-r--r-- permissions. At this point, you can restart your computer and enter ulimit -n into your terminal. If your system is configured correctly, you should see that maxfiles has been set to 200000.

How I modified limits for my 2013 Macbook air (running sierra): run sysctl -a | grep kern.maxfiles this may show you:

kern.maxfiles: 12288
kern.maxfilesperproc: 10240

I would presume this might be the kernels max capacity (probably guesstimated by apple, but still, I have to respect apple's settings). This seems like a fair max limit given my minimal research on this issue. For a soft limit, I think subtracting 2048 seems fair also. I'm no expert on this stuff, but maybe it sounds reasonable to you too.

12288-2048=10240 (soft limit) (Oh hey what do you know - this is the same number as the kern.maxfilesperproc).

The gist this was forked from set the macproc to 2048 which is lower than my current default 10240, so I'm not going to modify that for now.

My default max limit is acually unlimited right now when I run launchctl limit maxfiles but I'm going to err on the side of sticking to the instructions provided from basho.

If you get this error ulimit: setrlimit failed: invalid argument when trying to run ulimit - n 12288 in your shell/bash_profile?bashrc/zshrc, you need to include the second paramter for the hard limit, so I did ulimit -n 10240 12288 and this worked for me

<?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>10240</string>
<string>12288</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>ServiceIPC</key>
<false/>
</dict>
</plist>
#!/bin/sh
# These are the original gist links, linking to my gists now.
# curl -O https://gist.githubusercontent.com/a2ikm/761c2ab02b7b3935679e55af5d81786a/raw/ab644cb92f216c019a2f032bbf25e258b01d87f9/limit.maxfiles.plist
# curl -O https://gist.githubusercontent.com/tombigel/d503800a282fcadbee14b537735d202c/raw/ed73cacf82906fdde59976a0c8248cce8b44f906/limit.maxfiles.plist
# sudo mv limit.maxfiles.plist /Library/LaunchDaemons
# or
sudo vim /Library/LaunchDaemons/limit.maxfiles.plist
# paste in the code
sudo chown root:wheel /Library/LaunchDaemons/limit.maxfiles.plist
sudo launchctl load -w /Library/LaunchDaemons/limit.maxfiles.plist
@devinrhode2
Copy link
Author

devinrhode2 commented Dec 28, 2017

I did, however, not get an error when I edit the plist file and load it. I set the limits there to 64000 and 65536

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment