84

Before Mavericks, I could use /etc/launchd.conf file to change maximum system resource consumption, for example:

limit maxfiles 16384 unlimited
limit maxproc 16384 unlimited

It no longer works in Mavericks.

What is the correct way to do it in the recent version of OS X?

  • Your limits are above the max for OS X. maxfiles tops off at 10240 and maxproc limit is 1064. If the approved answer fixed the limits I would give it a thumbs up.. but it's been sitting there wrong and uncorrected for a good year + now.. – Joey T May 29 '15 at 14:50
  • @atmosx : It should be /etc/sysctl.conf, not /etc/sysctrl.conf. – Siu Ching Pong -Asuka Kenji- Aug 8 '15 at 15:02
  • 1
    @JoeyT : In fact you can set it to a higher limit! But you have to purchase "OS X Server" from "App Store" first if you're on Mac OS X 10.8 (Mountain Lion) or 10.9 (Mavericks). (Yes, you have to pay USD$19.99 to change a setting!) In short, you have to run sudo serverinfo --setperfmode true once to put your machine in "Server Performance Mode". Then, you have a "higher maximum", depending on you machine configuration. Please see my post at discussions.apple.com/thread/5166397 for details. For 10.10 (Yosemite), the mode is on by default (at least on my machine!). See my answer below. – Siu Ching Pong -Asuka Kenji- Aug 8 '15 at 15:19
84

Shell Session Limit

The limits set via ulimit only affects processes created by the current shell session.

  • The "soft limit" is the actual limit that is used. It could be set, as far as it's not greater than the "hard limit".
  • The "hard limit" could also be set, but only to a value less than the current one, and only to a value not less than the "soft limit".
  • The "hard limit", as well as system-wide limits, could be raised by root (the administrator) by executing system configuration commands or modifying system configuration files.

After you terminate the shell session (by Ctrl+D, exit, or closing the Terminal.app window, etc.), the settings are gone. If you want the same setting in the next shell session, add the setting to the shell startup script.

NOTE: If you are using bash, then it should be ~/.bash_proile or ~/.bash_login. If you are using other shells, it should probably be ~/.profile.

System Limit (Requires Reboot to Take Effect)

For 10.9 (Mavericks), 10.10 (Yosemite), 10.11 (El Capitan), and 10.12 (Sierra):

You have to create a file at /Library/LaunchDaemons/limit.maxfiles.plist (owner: root:wheel, mode: 0644):

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

You should change the numbers according to your needs. They are the "soft limit" (262144) and the is "hard limit" (524288) respectively. For more information, consult the manual page by running man launchd.plist.

For 10.8 (Mountain Lion):

You may add the following lines to /etc/sysctl.conf (owner: root:wheel, mode: 0644):

kern.maxfiles=524288
kern.maxfilesperproc=262144

You should change the numbers according to your needs. They are the "system-wide limit" (kern.maxfiles) and "per-process limit" (kern.maxfilesperproc) respectively. For more settings, consult the manual page by running man sysctl, or read the source code at /usr/include/sys/sysctl.h.

For older Mac OS X (I guess it works on 10.7 (Lion) or before):

You may add the following line to /etc/launchd.conf (owner: root:wheel, mode: 0644):

limit maxfiles 262144 524288

You should change the numbers according to your needs. They are the "soft limit" (262144) and the is "hard limit" (524288) respectively.

If the system doesn't let you set the limits above a certain value...

The system doesn't let you set a value higher than a "hard maximum" (proposed by Apple). To increase this "hard maximum", you have to purchase "OS X Server" from "App Store", then you have to execute the following command once:

sudo serverinfo --setperfmode true

This activates "server performance mode" on your machine. You can then set the maximum according to the configuration of your machine (see this). I tried this before (on Mountain and Mavericks) and it works! Please see my post (here) for more information.

References

  • 1
    Thank you; I'm using Yosemite and the the LaunchDaemon plist technique worked for me. I believe it's the only one that does. For anyone who needs this, please note that a reboot is required for it to take effect. – nc. Sep 22 '15 at 17:57
  • 1
    @nc. : Added "Require Reboot to Take Effect" to my answer. I have been working too long in the field, so I was not aware that some people do not know that. Thanks for reminding! – Siu Ching Pong -Asuka Kenji- Sep 22 '15 at 23:33
  • 2
    I'm using 10.9.5 but only the /etc/launchd.conf approach shows any effect when I run ulimit -n. Could someone update the instructions if this is significant? – user7000 Oct 5 '15 at 23:09
  • 1
    The 10.10 Yosemite method also works in 10.11 El Capitan. Although I couldn't set it to "unlimited" so I had to just use a really high number – antriver Oct 24 '15 at 23:37
  • 1
    @aolszowka: See this for how to increase that limit via /etc/sysctl.conf: superuser.com/questions/827984/… – Rob Johansen Jan 29 '16 at 18:42
11
+50

It seems that creating the file /etc/launchd.conf and putting your command inside it should do the trick.


If it does not work, you can probably edit or create the /etc/rc.local file and add your command inside it as there is little chance that Apple will ever delete support for limit on the command line.


Edit 1:
I should have start with that, the launchd man page reference the following files :

  ~/Library/LaunchAgents         Per-user agents provided by the user.
  /Library/LaunchAgents          Per-user agents provided by the administrator.
  /Library/LaunchDaemons         System-wide daemons provided by the administrator.
  /System/Library/LaunchAgents   Per-user agents provided by Mac OS X.
  /System/Library/LaunchDaemons  System-wide daemons provided by Mac OS X.

My bet is that you now need to put your command either in ~/Library/LaunchAgents or in /Library/LaunchDaemons.
You should try both.

Edit 2:
Be also aware that launchd need xml file and not only scripts. a gui has been deisgn to help in such task a not free one is Lingon. Maybe other free products exist.

  • as there is lot's of information in my post can you told which was the good one ? – Kiwy Jan 15 '14 at 21:41
  • see my answer below. – Howard May 27 '14 at 15:32
  • Both /etc/launchd.conf and /etc/rc.local are ignored in 10.10 and later, since in 10.10 launchd was redesigned and SystemStarter was removed. – nisetama Aug 29 '16 at 21:53
  • @nise feel free to update my answe'r if something new came up in recent version of Mac OS – Kiwy Aug 30 '16 at 9:51
10

I just added these two lines in my .bash_profile
works like a charm

ulimit -n 1024
ulimit -u 1024
  • 2
    Doesn't influence the system limits. – not2savvy Jun 23 '17 at 12:48
  • 1
    However, that might be okay if your system limits are high enough. To check what they are you can simply run ulimit -n -H. On macOS High Sierra it was unlimited so changing the user's "soft" limit in the .bash_profile does the trick. – Joshua Pinter Apr 15 '18 at 0:41
8

System limits

Changing the limits in /etc/launchd.conf or /etc/rc.local is no longer supported for the recent macOS. See: Old Systems and Technology.

Instead, you should create a new launch agent.

Here is the command example using PlistBuddy command (see: man PlistBuddy):

sudo /usr/libexec/PlistBuddy /Library/LaunchAgents/com.launchd.maxfiles.plist \
-c "add Label string com.launchd.maxfiles" \
-c "add ProgramArguments array" \
-c "add ProgramArguments: string launchctl" \
-c "add ProgramArguments: string limit" \
-c "add ProgramArguments: string maxfiles" \
-c "add ProgramArguments: string 10240" \
-c "add ProgramArguments: string unlimited" \
-c "add RunAtLoad bool true"

And similar for maxproc limit:

sudo /usr/libexec/PlistBuddy /Library/LaunchAgents/com.launchd.maxproc.plist \
-c "add Label string com.launchd.maxproc" \
-c "add ProgramArguments array" \
-c "add ProgramArguments: string launchctl" \
-c "add ProgramArguments: string limit" \
-c "add ProgramArguments: string maxproc" \
-c "add ProgramArguments: string 2000" \
-c "add ProgramArguments: string unlimited" \
-c "add RunAtLoad bool true"

To load above files, run: sudo launchctl load /Library/LaunchAgents/com.launchd.*.plist.

Notes:

  • To print the file, run: cat or PlistBuddy -x -c Print /Library/LaunchAgents/com.launchd.maxfiles.plist.
  • To check the log for any errors during load, run: tail -f /var/log/system.log.
  • To see the current launchd limits, run: launchctl limit.
  • The .plist file can be placed in per-user or system-wide agent folder (LaunchAgents). See: man launchd and man launchd.plist, or this or that answer for more details.

Kernel limits

Please note that above Launchd system limits are still limited by the kernel, so you can't set them higher than the actual limits set in kernel state variables (see: man sysctl for help).

To see the current kernel limits, run: sysctl -a | grep ^kern.max.

To increase the maxfiles limit, run: sudo sysctl -w kern.maxfiles=20480.

To make them persist, use the similar method to create a startup .plist files, e.g.

sudo /usr/libexec/PlistBuddy /Library/LaunchAgents/com.kern.maxfiles.plist \
-c "add Label string com.kern.maxfiles" \
-c "add ProgramArguments array" \
-c "add ProgramArguments: string sysctl" \
-c "add ProgramArguments: string -w" \
-c "add ProgramArguments: string kern.maxfiles=20480" \
-c "add RunAtLoad bool true"

Shell limits

For shell limits, add relevant ulimit command into ~/.bashrc or ~/.bash_profile startup file for individual user, or /etc/bashrc for all users. See: How to add persist shell ulimit settings on Mac?

Suggested lines to add:

# Changes the ulimit limits.
ulimit -Sn 4096      # Increase open files.
ulimit -Sl unlimited # Increase max locked memory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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