267

I need to get rid of the pesky "Too many open files in system" limit on OS X 10.7.1. 

Is there a way?

Oliver Salzburg
  • 89,072
  • 65
  • 269
  • 311
John Wilund
  • 2,687

13 Answers13

287

By default, the maximum number of files that Mac OS X can open is set to 12,288 and the maximum number of files a given process can open is 10,240.

You can check these with:

  • sysctl kern.maxfiles
  • sysctl kern.maxfilesperproc

You can increase the limits (at your own risk) with:

  • sysctl -w kern.maxfiles=20480 (or whatever number you choose)
  • sysctl -w kern.maxfilesperproc=18000 (or whatever number you choose)

To make the change permanent, use sudo to put your settings in /etc/sysctl.conf (which you may have to create), like this:

kern.maxfiles=20480
kern.maxfilesperproc=18000

Note: In OS X 10.10 or lower, you can add setting in /etc/launchd.conf like limit maxfiles and it will override whatever you put here.

Again, from the article:

Once you’ve done this, the kernel itself will have a maximum number of files but the shell might not. And since most processes that will take up this many files are going to be initiated by the shell you’re gonna want to increase that.

The command for that is:

ulimit -S -n 2048 # or whatever number you choose

That change is also temporary; it only lasts for the current shell session. You can add it to your shell configuration file (.bashrc, .zshrc or whatever) if you want it to run every time you open a shell.

Nathan Long
  • 27,435
82

It seems like there is an entirely different method for changing the open files limit for each version of OS X!

For OS X Sierra (10.12.X) you need to:

1. Create a file at /Library/LaunchDaemons/limit.maxfiles.plist and paste the following in (feel free to change the two numbers (which are the soft and hard limits, respectively):

<?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> 

2. Change the owner of your new file:

sudo chown root:wheel /Library/LaunchDaemons/limit.maxfiles.plist

3. Load these new settings:

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

4. Finally, check that the limits are correct:

launchctl limit maxfiles
ninjaPixel
  • 1,608
  • 14
  • 9
48

Other option may be finding the culprit:

sudo lsof -n | cut -f1 -d' ' | uniq -c | sort | tail

For the last one you could see what files are open:

sudo lsof -n | grep socketfil

And kill the process if so desired

kill $pid

From the comments:

For what it's worth, you can also get a list of the process IDs with the most open files using

lsof -n +c 0 | sed -E 's/^[^ ]+[ ]+([^ ]+).*$/\1/' | uniq -c | sort | tail
sanmai
  • 1,182
43

You will need to increase your ulimit settings - it's pretty low on OS X these days - 256 by default. Add ulimit -n 4096 or similar to your ~/.profile or equivalent and that will solve it in your local environment. Run ulimit -a to check your current levels

To see the system settings, run this:

launchctl limit maxfiles

It is set quite a bit higher in Lion (10240) on a per process basis than it used to be. But if you are still hitting it there then you can set it higher using the same command with the desired levels. To make the changes permanent /etc/launchd.conf is where you need to add the relevant lines.

Adam C
  • 2,765
14

Folks, on Mavericks 10.9.4

ulimit -n 2048 works fine. You may need to launch a new login session.

6

For latest macOS (at the time of writing: 10.14.1), you can use sudo launchctl limit maxfiles 64000 524288 (by default it was 256), but it works only within current session. Use launchctl job from @ninjaPixel (https://superuser.com/a/1171028/760235) for permanent solution.

Worthwelle
  • 4,816
5

Similar to https://superuser.com/a/1171028/367819

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.

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.


You can follow this article for more details.

https://gist.github.com/tombigel/d503800a282fcadbee14b537735d202c


Remember to restart your Mac to have the values effective.

angelokh
  • 151
3

After all changes above my java didn't made more that 10000 files. Solution was this jvm flag -XX:-MaxFDLimit

2

Increasing the number of authorized files read and write should never be considered as a good common practice.

As sanmai and many suggested you could simply try to kill some overwhelming processes. Probably the ones involved in the increase of number of file manipulations.

1-/ Step one retrieve the most file consuming processes

lsof -n +c 0 | cut -f1 -d' ' | uniq -c | sort | tail

2-/ Step two use theses names to kill all the processes they are involving. beware of not killing systematically any file consuming process since it might be essential for your current activity.

ps -e -a |grep file-consuming-process |for i in `awk {'print$1'}`; do kill -9 $i; done

repeat the process until you kill the undesired mighty daemons.

Have fun :)

osbor
  • 21
1

You can run

lsof -n

which process open too many files.

then kill it .

or

sysctl -w kern.maxfiles=20480

change it to bigger one.

0

I encountered it while doing a chmod -R so I got it around by taking smaller steps, e.g.

# for each directory
find . -type d -exec chmod 755 {} \;
0

I had this issue on a 2017 Macbook running Catalina. The issue was Spotify (which was up to date), and the Spotify setting "Show Local Files".

This setting allows Spotify to look through your downloads folder or other folders and for some reason it was opening and checking but not closing the files. Checking lsof -n this was actually over 7500 files including thousands of .jpg files as I had not cleaned out my downloads folder in a long while. Not sure why Spotify didn't close them, seems to be a bug.

Disabling this setting and restarting Spotify may work, alternatively just uninstalling will work.

tomglynch
  • 251
0

For me, most of the suggested answers worked after I have restarted my device and run the mongorestore with internet browsers, Spotify, Slack etc. are closed. I am guessing opening a new terminal shell or turning off the apps does not release the resources immediately.

Therefore, if you are gonna use the mongorestore more than once in the same session, I'd advice to follow my suggestion above.

Melih
  • 101