Is there a fix for the "Too many open files in system" error on OS X 10.7.1?

202

107

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?

John Wilund

Posted 2012-06-07T08:52:49.633

Reputation: 2 029

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 – 2014-09-19T07:55:59.460

You mentioned you're a developer; regardless of the defaults, this can also happen if a program opens files and leaves the file handles open. Make sure the software you're developing successfully closes each file it opens after it is done with it. – rob – 2016-12-14T18:59:26.927

In my case, I closed and reopened Terminal. Problem went away. – Mike S. – 2018-12-27T15:10:56.737

4Do you want to explain more about when this happens? In which circumstances? – slhck – 2012-06-07T08:56:55.150

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 – 2012-06-22T18:25:47.203

1Actually, 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 – 2012-06-29T20:16:00.153

2If 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 – 2013-06-13T18:06:07.450

Answers

234

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.

Nathan Long

Posted 2012-06-07T08:52:49.633

Reputation: 20 371

1I 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 – 2014-11-13T00:47:14.803

Note 10.10.3 changes this max https://twitter.com/hichaelmart/status/586681771485487104

– engineerDave – 2015-04-17T15:11:20.827

I come here to find this post at least once every 6 months or so! Awesome ;) – brandonscript – 2016-07-28T01:46:17.463

@brandonscript Thanks for saying so. :) Pro tip: I document this kind of stuff online after I figure it out partly so that I can Google it more easily next time I have the problem. Do likewise and maybe I'll find your answer to my next question. – Nathan Long – 2016-07-28T12:57:48.107

Heh I've got a handful of those on that other Stack Exchange site :) – brandonscript – 2016-07-28T14:05:24.560

2If 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 – 2016-10-14T16:27:42.227

@jjathman thanks for your comment about the trailing space, had that problem, removing it fixed it for me! – Ricky Nelson – 2017-01-30T11:32:24.600

Nathan, I do the same (entering solutions when I find them), also put it in a tweet. Should update the answer here noting the reboot into recovery mode needed to unlock sysctl as of 10.10.3 – Tracker1 – 2017-06-01T21:36:59.383

What's the equivalent for Catalina? – adib – 2019-10-15T14:24:41.303

1what 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 – 2012-08-15T00:48:08.497

@Cheeso - I think that the overall system limit (sysctl) or the launchd limit, whichever is lower, controls that. – Nathan Long – 2012-12-14T20:39:44.443

1creating an /etc/launchd.conf with contents limit maxfiles 1000000 1000000 worked great for me! (OSX 10.8.2 here) – Zugwalt – 2013-02-01T19:26:05.160

63

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

Posted 2012-06-07T08:52:49.633

Reputation: 1 361

worked perfectly, thanks! In my case the error manifested in a java process with the message IO Error: Bad file descriptor (Write failed) – agradl – 2017-03-22T16:52:14.020

1Also works on El Capitan 10.11.6 – Troy Daniels – 2017-04-17T17:23:18.790

still cannot change the ulimit for shell. The maximum stays 1024 whatever I do – DataGreed – 2017-10-06T00:46:32.227

At step 2 run: sudo chmod 600 /Library/LaunchDaemons/limit.maxfiles.plist sudo chown root /Library/LaunchDaemons/limit.maxfiles.plist – Hai Nguyen – 2018-06-21T08:00:07.057

34

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

sanmai

Posted 2012-06-07T08:52:49.633

Reputation: 622

Helpful! But sort on OS X (10.11) doesn't take -h. (Maybe -g?) – Robert Calhoun – 2017-03-03T14:52:20.000

For me worked well just without -h (OS X 10.12.3): sudo lsof -n | cut -f1 -d' ' | uniq -c | sort | tail – vearutop – 2017-03-06T03:42:28.107

So be it without -h – sanmai – 2017-03-06T10:27:23.380

This is the only answer that helped me to root cause my issue.. thanks :) – SgtPooki – 2017-03-11T10:46:46.267

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 – 2017-05-12T04:52:44.280

Reeder had 631 open! awacsd had 4297 which was probably the problem. – malhal – 2018-01-22T15:54:02.173

1Use lsof -n +c 0 to prevent truncating process name. – vaughan – 2018-07-12T11:45:19.323

34

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

Posted 2012-06-07T08:52:49.633

Reputation: 2 475

2Same for me, 256 files on MacOS X Maverick – Climbatize – 2014-08-09T06:44:06.527

4256 on OS X Yosemite as well – Alexander – 2015-05-28T09:52:48.927

2256 on El Capitan, too. – TMN – 2016-04-28T10:53:28.997

1256 in Yosemite. – Jaec – 2016-06-16T22:50:00.803

1256 still on Sierra. – ylluminate – 2017-04-06T18:53:44.203

ulimit -n 4096 won't work - it still remains 1024 although I don't have this limit anywhere – DataGreed – 2017-10-06T00:47:40.677

ulimit -n 4096 did not update the output of launchctl limit maxfiles , what should I do ? @Adam C – alper – 2018-08-02T20:58:12.390

ulimit still works on Mojave (10.14.6) too (in order to increase the shell maxfiles limit) – Oliboy50 – 2019-12-11T08:51:38.863

1256? It's 2560 file descriptors for me and I've never changed it. The limit is 266 processes (c.f. ulimit -a). – slhck – 2012-06-22T18:55:13.477

@slhck it says open files (-n) 256 for me too, not 2560 – Erik Aigner – 2013-05-25T10:43:01.537

Which version of OS X are you running? – nealmcb – 2014-05-26T18:19:46.560

10

Folks, on Mavericks 10.9.4

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

Clustermagnet

Posted 2012-06-07T08:52:49.633

Reputation: 349

5

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.

Dzmitry Hubin

Posted 2012-06-07T08:52:49.633

Reputation: 151

How did you come up with the number 524288? My previous values were 256 and unlimited. – Chip Roberson – 2019-10-06T21:39:45.307

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.

SaintKnight

Posted 2012-06-07T08:52:49.633

Reputation: 11

3Please explain how this answer differs from the ones already given. – Stephen Rauch – 2017-05-18T03:30:27.253

1

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

Uros Velickovic

Posted 2012-06-07T08:52:49.633

Reputation: 11

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 {} \;

Michael

Posted 2012-06-07T08:52:49.633

Reputation: 1

1While 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 – 2017-01-04T21:11:36.913

0

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

Posted 2012-06-07T08:52:49.633

Reputation: 101

Either the post should be marked as a duplicate if there is already an answer, otherwise, please post the relevant information from a link, as the link may not be valid forever. – zymhan – 2019-05-30T01:04:46.303