88

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?

3
  • 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, 2015 at 14:50
  • @atmosx : It should be /etc/sysctl.conf, not /etc/sysctrl.conf. Aug 8, 2015 at 15:02
  • 2
    @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. Aug 8, 2015 at 15:19

5 Answers 5

94

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

17
  • 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, 2015 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! Sep 22, 2015 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? Oct 5, 2015 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, 2015 at 23:37
  • 1
    @aolszowka: See this for how to increase that limit via /etc/sysctl.conf: superuser.com/questions/827984/… Jan 29, 2016 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.

4
  • as there is lot's of information in my post can you told which was the good one ?
    – Kiwy
    Jan 15, 2014 at 21:41
  • see my answer below.
    – Howard
    May 27, 2014 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, 2016 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, 2016 at 9:51
9

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

ulimit -n 1024
ulimit -u 1024
2
  • 2
    Doesn't influence the system limits.
    – not2savvy
    Jun 23, 2017 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. Apr 15, 2018 at 0:41
9

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.
2
  • 1
    Be particularly careful with maxproc because by default the hard kernel limit on that parameter is 1064, meaning setting it to 2000 or "unlimited" actually only sets it to 1064. Unfortunately, the kernel does not enforce that maxprocperuid < maxproc, and the settings you suggest will leave the system with maxprocperuid=2000 and maxproc=1064 which is dangerous. Read more about it here.
    – Old Pro
    Oct 20, 2019 at 2:04
  • This seems to be the only answer that identifies that there are actually THREE layers of limits, not just two. I needed to change both the "system" and "kernel" layers for it to work for me. Jun 11, 2021 at 21:06
1

If you have a single program that is hitting a ulimit (a soft limit on the number of files a single process can open), the adjusting the ulimit to a higher number is fine, especially if you can just put the ulimit command in your .bash_profile. Beyond that, I strongly advise against editing files like /etc/launchd.conf or /etc/sysctl.conf or adding plist files under /Library/LaunchDaemons/ for a few reasons.

  • Changes to those files persist in backups, and are carried over to new versions of macOS and new computers when you upgrade.

  • If those changes cause problems (which is a real possibility), you have to remember that you made the changes and what the changes are and then re-edit the files to undo them. This can happen years later.

  • After a few years of upgrading, you may find that what used to be an increase in a limit is now a decrease in a limit. But you probably will not know that because you (a) will not remember that you made the change and (b) will not get to see the new limits because you overrode them from the start.

A Much Better Option

In general, rather than tuning individual parameters on the system and throwing the system out of balance (and potentially letting a single program crash the system by taking up all the resources), if the default system limits are insufficient for your needs, I recommend turing on "Server Performance Mode", or at least giving it a try. All you need for this is OS X / macOS 10.8 Mountain Lion or later and at least 16 GiB of memory installed. Early on you had to pay for this, but starting with OS X 10.8 Mountain Lion, it is free and officially supported by Apple with the standard OS.

Turning on this mode dramatically increases system limits, particularly the number of processes you can run and the number of files you can have open, at the cost of allocating more memory to the system kernel. You can read in detail what is changed by server performance mode in the answer to the question "What does serverperfmode=1 actually do on macOS?".

This mode has several advantages over editing configuration files as suggested by other answers.

  • It is a change of a single parameter, easy to undo.
  • It results in a balanced set of higher limits, preserving safeguards against rogue processes crashing the system.
  • In most cases, it provides higher limits than the kernel would otherwise allow, even with editing configuration files.
  • It raises default limits for all processes without any additional configuration changes.
  • Starting with OS X 10.11 El Capitan, the configuration change is stored in NVRAM, which means if it is causing problems, you will revert to normal mode when you take the troubleshooting step of resetting NVRAM. You don't have to remember it.
  • Because it is in NVRAM, it will not accidentally change state when restoring from backups.
  • Because it is in NVRAM, it will not automatically come on when you clone your Mac setup to new hardware.
  • Because it is officially supported by Apple, "it just works" and you will not get complaints from Apple Geniuses that you improperly modified the system when you ask for support.
  • Because it is a change of a single parameter, you can easily turn it on and off for troubleshooting or comparison of one setting to the other.

Turn Server Performance Mode On or Off

To turn on Server Performance Mode, use the Terminal to run one of these commands and then reboot for it to take effect:

  • For OS X 10.11 El Capitan or later, turn it on with
sudo nvram boot-args="serverperfmode=1 $(nvram boot-args 2>/dev/null | cut -f 2-)"

and turn it off with

sudo nvram boot-args="$(nvram boot-args 2>/dev/null | sed -e $'s/boot-args\t//;s/serverperfmode=1//')"

The above commands are what Apple officially recommends, but there is actually a problem with them, which is that if you run the "turn on" command twice, you then have to run the "turn off" command twice to turn it off. So check to see if it is on or off after making a change by running

nvram boot-args

If the output includes "serverperfmode=1" then the setting is on, and if it does not, then the setting is off.

  • For OS X 10.8 Mountain Lion, OS X 10.9 Mavericks, and OS X 10.10 Yosemite, turn server performance mode on with
serverinfo --setperfmode 1

and turn it off with

serverinfo --setperfmode 0

Check the setting with

serverinfo --perfmode

The setting will not take effect until after you reboot the system.

Checking To See If The Computer Is Currently Running In Server Performance Mode

Checking the setting will tell you whether it is set to take effect (or not) after rebooting. To test to see if it currently active (assuming you have followed my advice and not edited any configuration files that change the settings), run

sysctl kern.maxproc

It will give you a number that is the maximum number of processes the system will allow. If that number is a multiple of 532, then server performance mode is off. If it is a round number (a multiple of 2500), then server performance mode is on for the currently running system.

2
  • I don't agree with the last sentence. After having set server performance mode on, I get kern.maxproc: 4096 here.
    – not2savvy
    Dec 4, 2020 at 17:02
  • 1
    Agreed that serverperfmode is the best way to do this! Unfortunately, serverperfmode is not supported on M1 Macs, as of June 2021. If you'd like Apple to add it back in, I suggest you filing a request via Feedback Assistant so they know you care! Jun 11, 2021 at 21:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .