The maximum number of connections for Mac and the settings of related parameters of the port
2014-11-18
Some time ago, I paid attention to the concurrent performance of the server, and I simply studied some related parameter settings of Mac, and recorded it here.
Maximum number of connections
The maximum number of connections is the limit of the maximum number of files (file descriptors) that the system can open. It is divided into two types: global and process. The corresponding commands are as follows:
$ sysctl kern.maxfiles
Output: kern.maxfiles: 12288
Description: Global limit, that is, the default maximum number of connections in the system is 12288
$ sysctl kern.maxfilesperproc
Output: kern.maxfilesperproc: 10240
Description: The default maximum connection limit for a single process is 10240
$ sudo sysctl -w kern.maxfiles=1048600
Output: kern.maxfiles: 12288 -> 1048600
Description: Set the maximum number of connections in the system from 12288 to 1048600
$ sudo sysctl -w kern.maxfilesperproc=1048576
Output: kern.maxfilesperproc: 10240 -> 1048576
Description: Set the limit on the number of process connections, the maximum number of connections of a process should be less than or equal to the number of global connections
ulimit command
$ ulimit -n
Output: 2560
Description: The "ulimit -n" command displays the maximum number of files that the current shell can open, the default value: 2560, which is always less than the value of kern.maxfilesperproc, because a shell is a process.
$ ulimit -n 1048576
Description: Set the maximum number of files that the current shell can open to 1048576. This value cannot be greater than kern.maxfilesperproc, otherwise the setting fails.
dynamic port range
$ sysctl net.inet.ip.portrange
Output:
net.inet.ip.portrange.first: 49152
net.inet.ip.portrange.last: 65535
The default range of Linux dynamic port numbers is 32768-65535, that is to say, as a client connecting to the same IP and the same port number, only more than 30,000 connections can be established at most, while Mac can only establish about 16,000 connections by default.
$ sysctl -w net.inet.ip.portrange.first=32768
Description: Set the dynamic allocation starting port number to 32768, which can increase the number of connections that the client can establish.
question
There is a problem with setting the parameters in the above way. When the system restarts, these parameters are restored to their default values. The solution is to write the parameters to the /etc/sysctl.conf file. However, this file does not exist by default. So first create it:
sudo touch /etc/sysctl.conf
Then write the parameters to the file
kern.maxfiles=1048600
kern.maxfilesperproc=1048576
net.inet.ip.portrange.first=49152
net.inet.ip.portrange.last = 65535
Restart the system, check the result, it shows success.
As for ulimit-n
the value, you can ulimit-n 1048576
write it to .bashrc to achieve automatic modification.
Reprinted from: http://tinylee.info/mac-maxfiles-portrange.html