Where are the default ulimits specified on OS X (10.5)?

The default nofile limit for OS X user accounts seems to be about 256 file descriptors these days. I'm trying to test some software that needs a lot more connections than that open at once.

On a typical Debian box running the pam limits module, I'd edit /etc/security/limits.conf to set higher limits for the user that will be running the software, but I'm mystified where to set these limits in OS X.

Is there a GUI somewhere for it? Is there a config file somewhere for it? What's the tidiest way to change the default ulimits on OS X?


Solution 1:

Under Leopard the initial process is launchd. The default ulimits of each process are inherited from launchd. For reference the default (compiled in) limits are

$ sudo launchctl limit
    cpu         unlimited      unlimited      
    filesize    unlimited      unlimited      
    data        6291456        unlimited      
    stack       8388608        67104768       
    core        0              unlimited      
    rss         unlimited      unlimited      
    memlock     unlimited      unlimited      
    maxproc     266            532            
    maxfiles    256            unlimited

To change any of these limits, add a line (you may need to create the file first) to /etc/launchd.conf, the arguments are the same as passed to the launchctl command. For example

echo "limit maxfiles 1024 unlimited" | sudo tee -a /etc/launchd.conf

However launchd has already started your login shell, so the simplest way to make these changes take effect is to restart our machine. (Use >> to append to /etc/launchd.conf.)

Solution 2:

Shell limits

Resources available to the shell and processes can be changed by ulimit command which can be added to startup scripts such as ~/.bashrc or ~/.bash_profile for individual users or /etc/bashrc for all users. Example line to add:

ulimit -Sn 4096 && ulimit -Sl unlimited

See: help ulimit and man bash for more information.

System limits

In general, system limits are controlled by Launchd framework and can be changed by launchctl command, e.g.

launchctl limit maxfiles 10240 unlimited

To make the changes persistent, you need to create a property list file in specific Launch compliant folders which acts as a startup agent.

Here is the example command creating such startup file:

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"

The file would be loaded at the system launch, however, to load to manually run:

sudo launchctl load /Library/LaunchAgents/com.launchd.maxfiles.plist

To verify the current limits, run: launchctl limit.

See: Creating Launch Daemons and Agents.

Kernel limits

  • Kernel limits are controlled by the sysctl command.
  • To see the current kernel limits, run: sysctl -a | grep ^kern.max.
  • To change the maximum of files allowed to open, run: sudo sysctl -w kern.maxfiles=20480.
  • To make the changes persistent, use similar above method to create the property list file in system startup folder.

Related:

  • How to persistently control maximum system resource consumption on Mac?
  • Which command controls the open file limits?

Deprecated methods

In earlier version of macOS, you could set these limits in /etc/sysctl.conf system-wide as normally you do on Unix, however, it seems it is not supported.

Using ~/.launchd.conf or /etc/launchd.conf appears that it is also not supported in any existing version of macOS either.wiki

Same with /etc/rc.local startup file, it is not supported on macOS.

Solution 3:

sudo echo "limit maxfiles 1024 unlimited" >> /etc/launchd.conf

does not work because sudo is in the wrong place, try this:

echo 'limit maxfiles 10000 unlimited' | sudo tee -a /etc/launchd.conf