How to open more than 10240 files per process in mac?

Viewed 732

I have changed the value "max open files" to 655350. But I still got the "Too many open files" exception.

The version of my macos is 10.15.6

My test code:

public class MaxOpenFiles {
    public static void main(String[] args) throws IOException {
        OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
        List<Socket> sockets = new ArrayList<>();
        for (int i = 0; i < 20000; i++) {
            sockets.add(new Socket("localhost", 8080));
            if(os instanceof UnixOperatingSystemMXBean){
                System.out.println(("Number of open fd: " + ((UnixOperatingSystemMXBean) os).getOpenFileDescriptorCount()));
            }
        }
    }
}

Result:

enter image description here

1 Answers

Give a read to http://krypted.com/mac-os-x/maximum-files-in-mac-os-x/

  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>
  1. Change the owner of your new file:
sudo chown root:wheel /Library/LaunchDaemons/limit.maxfiles.plist
sudo chmod 600 /Library/LaunchDaemons/limit.maxfiles.plist
  1. Load these new settings:
sudo launchctl load -w /Library/LaunchDaemons/limit.maxfiles.plist
  1. Finally, check that the limits are correct:
launchctl limit maxfiles

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.

Additionally, you can try:

sudo launchctl limit maxfiles 64000 524288

(by default it was 256), but it works only within current session.

Use launchctl job for a permanent solution.

References:

  1. @ninjaPixel (https://superuser.com/a/1171028/760235)
Related