MacOS High Sierra and “Too Many Open Files”

While preparing next version of Dinamica EGO for Mac, I was facing the “Too Many Open files” error with a couple of boost::filesystem::stream‘s. Turns out, Apple limits the number of handles you can open in every application (in my installation, up to 256 handles). You can check how many handles your process support by using:

ulimit -a

and checking the “open files” line. To overcome this limitation, I found this awesome blog post, which is still valid on MacOS High Sierra.

To prevent myself (and readers) from losing the details, I am copying the important bits from Dejan Kitic blog:

Create the file /Library/LaunchDaemons/limit.maxfiles.plist with the 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.maxfiles</string>
    <key>ProgramArguments</key>
    <array>
      <string>launchctl</string>
      <string>limit</string>
      <string>maxfiles</string>
      <string>524288</string>
      <string>524288</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>ServiceIPC</key>
    <false/>
  </dict>
</plist>

and the file /Library/LaunchDaemons/limit.maxproc.plist with the 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>

Then, change the ownership of both files:

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

And restart. Credit goes to Dejan Kitic and Gilles Celli for the solution.

Leave a Reply

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