Linux allows parameter tuning to open maximum file handles-"Too many open files" issue

Source: Internet
Author: User

All know the characteristics of the Linux system, all files, all in the run Zabbix such services, one of the important tuning is to adjust the maximum file handle of the Linux system, to solve the "too many open files" problem, increase the number of programs allowed to open the program to improve performance.

A To view the number of file handles open for the system to run
# ulimit -acore file size          (blocks, -c) 0data seg size           (kbytes, -d) unlimitedscheduling priority             (-e) 0file size               (blocks, -f) unlimitedpending signals                 (-i) 7179max locked memory       (kbytes, -l) 64max memory size         (kbytes, -m) unlimitedopen files                      (-n) 1024pipe size            (512 bytes, -p) 8POSIX message queues     (bytes, -q) 819200real-time priority              (-r) 0stack size              (kbytes, -s) 8192cpu time               (seconds, -t) unlimitedmax user processes              (-u) 7179virtual memory          (kbytes, -v) unlimitedfile locks                      (-x) unlimited

By default, the open files parameter is 1024, which is the maximum number of files that Linux allows to open. When running a service that requires a large concurrent process to run, this is obviously not enough, and will report "Too many open files". In the process of using Zabbix, when the number of open processes will be unable to start the situation, the production of the log will have a similar error, you need to modify the maximum number of file handles.

Two Modify maximum file handle-temporary modification
# ulimit -n 2048# ulimit -acore file size          (blocks, -c) 0data seg size           (kbytes, -d) unlimitedscheduling priority             (-e) 0file size               (blocks, -f) unlimitedpending signals                 (-i) 7179max locked memory       (kbytes, -l) 64max memory size         (kbytes, -m) unlimitedopen files                      (-n) 2048pipe size            (512 bytes, -p) 8POSIX message queues     (bytes, -q) 819200real-time priority              (-r) 0stack size              (kbytes, -s) 8192cpu time               (seconds, -t) unlimitedmax user processes              (-u) 7179virtual memory          (kbytes, -v) unlimitedfile locks                      (-x) unlimited

You can see that the open files parameter is adjusted to 2048, which only works for the current process. If you reopen a terminal or restart the process, this parameter will not take effect, so it is not recommended.

Three Modify maximum file handle-kernel parameter modification

Before setting, we must take into account the limitations of the system, if you modify the parameters more than the system default limits, will bring another tragedy, this pit stepped over.

(1) system default maximum value

File-max is the maximum number of files that can be allocated by the kernel

# cat /proc/sys/fs/file-max 181044

Nr_open is the maximum number of files that a single process can allocate

# cat /proc/sys/fs/nr_open 1048576

The average system default kernel can allocate the maximum number of files is about 10% of the memory, can be adjusted to about 50%.

# grep MemTotal /proc/meminfo |awk ‘{printf("%d",$2/10)}‘186726
(2) Adjust the maximum value

To modify the maximum allowable values in the two kernel files, it is important to note that the parameters in the Nr_open are smaller than the parameters in File-max.
The maximum allowable value of the system is adjusted to 50% of memory.

The maximum value that a single process can allocate increases appropriately.

# echo 233632 > /proc/sys/fs/nr_open
(3) modifying system kernel Parameters
# vim /etc/security/limits.conf*   soft     nofile      65535*   hard     nofile      65535
# vim /etc/security/limits.d/20-nproc.conf*   soft     nproc       65535*   hard    nproc       65535

Here the "*" sign for all users to take effect, you can set the specified user, modified to save the exit, take a look at a new terminal to take effect.

(4) Summary

A. The number of file descriptors opened by all processes must not exceed/proc/sys/fs/file-max
B. The number of file descriptors opened by a single process cannot exceed Nofile soft limit in user limit
C.nofile's soft limit cannot exceed its hard limit
D. Nofile's hard limit cannot exceed/proc/sys/fs/nr_open

(5) Special reminders

There is an unexpected situation, if you do not pay attention to modify the system default allowed maximum value, The parameters set in the limits.conf are larger than the system defaults, after the launch of the terminal, you will find that SSH can not link the tragedy, if you have not closed the terminal link, then congratulate you also have the room to save, modify the sshd configuration file.

# vim /etc/ssh/sshd_configUsePAM yes 将这里的yes改为no

Restarting the SSHD service
#systemctl restart sshd.service
At this point, you can link the terminal, adjust the maximum allowable value of the system kernel, and then change back to sshd configuration.

Four View commands for opening files (1) View the number of open files for all processes

# lsof |wc -l

(2) View the current number of file handles used by the entire system

# cat /proc/sys/fs/file-nr

(3) View the process that a process is opening

#lsof -p pid

(4) View the number of file handles for a process

#lsof -p pid|wc -l

(5) View a directory where the file is occupied by what process

#lsof path(file)

Linux allows parameter tuning to open maximum file handles-"Too many open files" issue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.