In the Linux production environment, you need to lift the limit on the number of file handles. The reason is that the default value of Linux is 1024, which means that a process can accept up to 1024 socket connections, which is far from enough.
File handles are also called file descriptors. In Linux, files can be classified into common files, directory files, link files, and device files.
A File Descriptor is an index created by the kernel to efficiently manage files that have been opened. It is a non-negative integer (usually a small integer) and is used to refer to the open File. All IO system calls (including read and write calls to sockets) are done through file descriptors.
On Linux, you can see the maximum number of file handles a process can open by calling the ulimit command. This command can be used as follows:
ulimit -n
Copy the code
The ulimit command is used to display and modify the basic limit of the current user process. The -n option is used to reference or set the limit of the current number of file handles. The default value on Linux is 1024.
If the number of open File handles in a single process exceeds the upper limit, the Socket/File:Can’t open so many files error message is displayed.
You can set these two parameters with ulimit as follows:
ulimit -n 1000000
Copy the code
One drawback of using the ulimit command is that it can only modify some of the basic restrictions of the current user environment, only for the current user environment. That is, the changes are valid for as long as the current terminal tool is connected to the current shell, and once the user is disconnected or the user exits Linux, it changes back to the system default value of 1024. After the system restarts, the number of handles is restored to the default value.
The ulimit command can only be used for temporary changes. If you want to permanently save the maximum number of file descriptors, you can edit the /etc/rc.local boot file and add the following contents to the file:
ulimit -SHn 1000000
Copy the code
The -s option indicates a soft limit and -h indicates a hard limit.
The hard limit is the actual limit, which can be 1 million, no more.
The soft limit is the limit at which the system will issue a Warning. If the limit is exceeded, the kernel will issue a Warning.
A common user can run the ulimit command to change the soft limit to the hard limit. If you want to change hard limits, you must have root privileges.
Thoroughly remove Linux system maximum file to open the number of restrictions, can be edited by the limit of the Linux configuration file/etc/security/limits. Conf. Modify this file by adding the following:
soft nofile 1000000
hard nofile 1000000
Copy the code
When using and installing the currently popular distributed search engine ElasticSearch, this file must be modified to increase the limit of the maximum file descriptor. In production run Netty, of course, also need to modify the/etc/security/limits file to increase the number of the file descriptor limit.