May 2024
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

Categories

May 2024
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

Too many open files

First, the problem found

Just on the line of the project, the front end of the use of LVS + Haproxy do load balancing, support for high concurrent traffic, but after a period of time is always a problem, view the log, found the following Too many open files problem.

May 12, 2017 12:49:20 AM org.apache.tomcat.util.net.JIoEndpoint$Acceptor run
SEVERE: Socket accept failed
java.net.SocketException: Too many open files
        at java.net.PlainSocketImpl.socketAccept(Native Method)
        at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:398)
        at java.net.ServerSocket.implAccept(ServerSocket.java:530)
        at java.net.ServerSocket.accept(ServerSocket.java:498)
        at org.apache.tomcat.util.net.DefaultServerSocketFactory.acceptSocket(DefaultServerSocketFactory.java:60)
        at org.apache.tomcat.util.net.JIoEndpoint$Acceptor.run(JIoEndpoint.java:222)
        at java.lang.Thread.run(Thread.java:745)

Second, the maximum number of files to open the description

1, first popularize a few knowledge

  • Linux everything is a file, including input and output devices, network connections, socket, pipes and so on;
  • And file opening number is the most relevant file descriptor (some people like to call the file identifier, English for the file descriptor), the number of file open is the number of file descriptors;
  • The number of open files depends on the type of system, memory size, int (language keyword, such as C99 int) length (non-negative integer), and system administrator settings;
  • The maximum number of file open is for a process, that is, a process can open the number of file handles is limited, can not exceed the maximum number of file open;
  • Ulimit command is only valid for the current shell, so when writing shell script, if you need and can control the maximum number of files open, then the implementation of “ulimit-n file open number” command, in the implementation of the following;
  • The file descriptors that are opened in Linux are stored in / proc / PID / fd /, where PID is the process identifier.

In addition to the need to pay attention, but also need to pay attention to ulimit -v unlimited, the largest available virtual memory (the maximum amount of virtual memory available to the shell and, on some systems, to its children)

2, the maximum number of files to open the global settings

In CentOS and Ubuntu ulimit is a bash inside the built-in command, like if, shift the same, not a separate command, so in Ubuntu usually encounter someone using sudo ulimit -n 65535 command encountered This command prompt (perhaps sudo bug),

Ulimit provides control of the shell or process available resources, including but not limited to the maximum number of file open, maximum available virtual memory, the maximum number of processes, socket buffer, etc., it has two levels of hard and soft, respectively, the corresponding parameter switch The -H and -S, hard limits make the non-root user not add (over) the set value, the soft limit allows the non-root user to increase the hard limit, usually the hard and soft values ??are usually set to the same Value, the command is ulimit -HSn 65535.

Ulimit is only valid for the current shell, in order to take effect anywhere, in addition to the first implementation of the ulimit command is to change the configuration file, that is, change the maximum number of files open the global settings, the method is to edit /etc/security/limits.conf file, add The following two lines, re-login system to take effect.

*  hard  nofile  65535
*  soft  nofile  65535

Among them, “*” means that all users are effective, restart, run anywhere ulimit-n will show 65535.

3, some of the commands associated with the opening of the file and other related commands

  1. View the current system of the total number of files opened: cat / proc / sys / fs / file-max
  2. View the current process of the file to open the number: lsof-p 16075 | wc-l
  3. View the current port of the file to open the number: lsof -i: 80 | wc-l
  4. Before using lsof need to pay attention, lsof is not suitable for viewing a high number of connections or the number of dynamic changes too fast process or port
  5. View the files used by a process: lsof -p 16075
  6. View the file used by a port: lsof -i: 80
  7. View users and programs that use a file: fuser -v / bin / bash

Third, pay attention to matters

1, the use of ulimit-n 65535 can take effect immediately, but after re-login will be restored to the initial system settings 1024.

2, you can also / etc / profile in the final configuration ulimit-n 65535, to take effect.

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>