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?

  • 4
    Do you want to explain more about when this happens? In which circumstances? – slhck Jun 7 '12 at 8:56
  • 1
    @slhck - I have the same problem. The circumstances are basically "at random." I'm a developer, so I'm using my Mac fairly heavily: running one or more databases, a web server, testing tools, one or more browsers, and a music player all at once. Google Chrome seems to be one program that has a lot of files open. – Nathan Long Jun 22 '12 at 18:25
  • Actually, my "heavy use" wasn't the issue; my settings for the maximum number of open files for the kernal and per-process were far lower than what the defaults should be. – Nathan Long Jun 29 '12 at 20:16
  • 2
    If your read Nathan's comment and wondered why he didn't include any details about the defaults, it's because he spelled it all out in his answer, below. (Nice answer! :) – Olie Jun 13 '13 at 18:06
  • I'm in the same usage circumstance as Nathan Long, and found restarting Apache was the only step that "solved" the problem. I applied all the below limit increases but they didn't help immediately. I am running command line phpUnit tests > selenium server > firefox > apache > php > mysql all on the same macbook. Used to work fine until I upgraded to mavericks. The error I get is in the webapp being tested, i.e. it's php/apache running out of files, so presumably not controlled by the shell setting. – scipilot Sep 19 '14 at 7:55

According to this helpful article (which I recommend reading):

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.

  • 1
    what limit applies to processes launched by clicking icons in the launch area? And how to change that limit? When you say "shell", I'm assuming you mean an interactive terminal shell. – Cheeso Aug 15 '12 at 0:48
  • @Cheeso - I think that the overall system limit (sysctl) or the launchd limit, whichever is lower, controls that. – Nathan Long Dec 14 '12 at 20:39
  • 1
    creating an /etc/launchd.conf with contents limit maxfiles 1000000 1000000 worked great for me! (OSX 10.8.2 here) – Zugwalt Feb 1 '13 at 19:26
  • 1
    I put kern.maxfiles=65000 kern.maxfilesperproc=65000 in /etc/sysctl.conf and rebooted. kern.maxfiles was ignored and stayed the default but kern.maxfilesperproc was set to 65000. I have no /etc/launchd.conf so what's up with that? – pferrel Nov 13 '14 at 0:47
  • 1
    If anyone has problems with max files not sticking, it is because there is a trailing space after the maxfiles line, that needs to be deleted. – jjathman Oct 14 '16 at 16:27

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
  • worked perfectly, thanks! In my case the error manifested in a java process with the message IO Error: Bad file descriptor (Write failed) – agradl Mar 22 '17 at 16:52
  • 1
    Also works on El Capitan 10.11.6 – Troy Daniels Apr 17 '17 at 17:23
  • still cannot change the ulimit for shell. The maximum stays 1024 whatever I do – DataGreed Oct 6 '17 at 0:46
  • At step 2 run: sudo chmod 600 /Library/LaunchDaemons/limit.maxfiles.plist sudo chown root /Library/LaunchDaemons/limit.maxfiles.plist – Hai Nguyen Jun 21 at 8:00

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.

  • 1
    256? It's 2560 file descriptors for me and I've never changed it. The limit is 266 processes (c.f. ulimit -a). – slhck Jun 22 '12 at 18:55
  • 2
    Same for me, 256 files on MacOS X Maverick – Climbatize Aug 9 '14 at 6:44
  • 4
    256 on OS X Yosemite as well – Alexander May 28 '15 at 9:52
  • 2
    256 on El Capitan, too. – TMN Apr 28 '16 at 10:53
  • 1
    256 in Yosemite. – Jaec Jun 16 '16 at 22:50

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 | sed -E 's/^[^ ]+[ ]+([^ ]+).*$/\1/' | uniq -c | sort | tail
  • Helpful! But sort on OS X (10.11) doesn't take -h. (Maybe -g?) – Robert Calhoun Mar 3 '17 at 14:52
  • For me worked well just without -h (OS X 10.12.3): sudo lsof -n | cut -f1 -d' ' | uniq -c | sort | tail – vearutop Mar 6 '17 at 3:42
  • So be it without -h – sanmai Mar 6 '17 at 10:27
  • This is the only answer that helped me to root cause my issue.. thanks :) – SgtPooki Mar 11 '17 at 10:46
  • For what it's worth, you can also get a list of the process IDs with the most open files using lsof -n | sed -E 's/^[^ ]+[ ]+([^ ]+).*$/\1/' | uniq -c | sort | tail. – Chris Frederick May 12 '17 at 4:52

Folks, on Mavericks 10.9.4

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

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.

  • 2
    Please explain how this answer differs from the ones already given. – Stephen Rauch May 18 '17 at 3:30

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 {} \;
  • 1
    While this may be a work-around, it doesn't appear to actually answer the question. Perhaps explaining that you cannot get rid of the message and then proposing this as one way to make it less of an issue would improve your answer. – music2myear Jan 4 '17 at 21:11

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.