How to handle Too many open files under Linux?

  

When doing Linux system operation, sometimes it will jump out too many open files prompt, that is, open too many files, it may be that you did not close normally when you close the program, the following small series will teach you to encounter too Many open files prompt how to solve.

answer a,

[root @ lxadmin nginx] # cat /proc /sys /fs /file-max

8192

File The maximum number of open files in the system

[root@lxadmin nginx]# ulimit -n

1024

Program limit can only open 1024 files

[root@lxadmin nginx]# ulimit -n 8192 Adjust

or permanently adjust the number of open files to be added at the end of the startup file /etc/rc.d/rc.local (at the end of /etc/sysctl.conf) Add fs.file-max=xxx is invalid)

ulimit -n 8192

Answer 2,

The Linux kernel sometimes reports "Too many open files", the cause is The file-max default (8096) is too small. To solve this problem, you can execute the following commands as root (or add them to the init script under /etc/rcS.d/*.)

# echo “65536” 》 /proc/sys/fs/File-max # for 2.2 and 2.4 kernels

# echo “131072” 》 /proc/sys/fs/inode-max #only for version 2.2 kernel

Answer 3

The method is to modify the operating system's open file limit, as follows:

1. Set the system according to the maximum number of open files, and check /proc/sys/fs/file- The max file confirms that the maximum number of open files has been set correctly.

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

If the setting is too small, modify the /etc/sysctl.conf variable to the appropriate value. This will take effect after each reboot. If the setting is large enough, skip the next step.

# echo 2048 》 /proc/sys/fs/file-max

Edit the file /etc/sysctl.conf and insert it into the downstream.

fs.file-max = 8192

2. Set the maximum number of open files in the /etc/security/limits.conf file. Here is a line:

#

Add the following line.

* - nofile 8192

This line sets the default open file number for each user to 2048. Note that the "nofile" option has two possible restrictions. It is the hard and soft under the item. In order for the modified maximum open file count to take effect, these two restrictions must be set. If the “-” character setting is used, the hard and soft settings will be set at the same time.

The hard limit indicates the maximum value that can be set in the soft limit. The soft limit refers to the setting value that the current system is in effect. The hard limit value can be lowered by ordinary users. But can't increase. The soft limit cannot be set higher than the hard limit. Only the root user can increase the hard limit value.

When adding a file limit description, you can simply double the current value. The example is as follows, if you want to increase the default value of 1024, it is best to increase to 2048, if you want to continue to increase, you need to set to 4096.

Another situation is that when creating an index, there are two possibilities. One is that the merge factor is too small, resulting in the number of created files exceeding the operating system limit. In this case, the merge factor can be modified, and the operation can be modified. The number of open files in the system is limited; the other is that the consolidation factor is limited by the virtual machine memory and cannot be adjusted to a larger size. The number of docs that need to be indexed is very large. At this time, only the number of open files of the operating system can be modified. The limit is solved.

Based on this, I also modified one of the following configuration files

vi /etc/sysctl.conf

Add:

# Decrease the time Default value for tcp_fin_timeout connection

net.ipv4.tcp_fin_timeout = 30

# Decrease the time default value for tcp_keepalive_time connection

net.ipv4.tcp_keepalive_time = 1800

# Turn off tcp_window_scaling

net.ipv4.tcp_window_scaling = 0

# Turn off the tcp_sack

net.ipv4.tcp_sack = 0

# Turn off tcp_timestamps

net.ipv4.tcp_timestamps = 0

Then service network restart, these are optimizations related to TCP sockets.

Also need to be added in /etc/rc.d/rc.local has been made to take effect when the restart.

echo “30”"/proc/sys/net/ipv4/tcp_fin_timeout

echo “1800”"/proc/sys/net/ipv4/tcp_keepalive_time

Echo “0”"/proc/sys/net/ipv4/tcp_window_scaling

echo “0&0&quo;"/proc/sys/net/ipv4/tcp_sack

echo “0” /proc/sys/net/ipv4/tcp_timestamps

Because not all programs run under root, all Linux has a distinction between hard and soft open files. Ordinary users are limited by hard, no matter ulimit - The n value is adjusted to a high value, and the value of nofile in /etc/security/limits.conf cannot be reached.

After this optimization lsof -p $java_pid| Wc -l can run more than 4,000 and will not throw too many open files.

The above is the introduction of the solution to the too many open files prompt. You can solve this problem by changing the system settings, closing the redundant programs, etc. Is it very simple? Try it out.

Copyright © Windows knowledge All Rights Reserved