The following is my understanding and summary of the knowledge that I have learned when I solve the too many open files exception, if there is an incorrect point, please point out!
This problem is similar to Chinese search, you can try the following keywords: "File descriptor Leak" "StackOverflow" "How To Solve open files exception" and so on.
Here is some of my summary, perhaps to you some help!
1.fd
FD is short for file descriptor
In a Linux environment, everything is in the form of files, and files are not only accessible to regular data, but also to network connections and hardware.
The application is identifying the file/device/service through FD.
You may need to know more about the definition of FD and its functional knowledge.
2.lsof
Commands under Linux, full name: List system open files
The 1th section says that all Linux resources are in file form, so this lsof command is a powerful tool for viewing system resource usage. Troubleshoot various resource depletion, exceptions under Linux
You can use it for all problems. Almost all Chinese can find this kind of http://www.cnblogs.com/ggjucheng/archive/2012/01/08/2316599.html, the following is the reference:
Enter lsof under Terminal to display the system open file, because lsof needs to access core memory and various files, so it must be run as root user to be able to charge
To perform its functions. The direct input lsof partial output is:
COMMAND PID USER FD TYPE DEVICE size/off NODE NAME
Init 1 root cwd DIR 8,1 4096 2/
Init 1 root RTD DIR 8,1 4096 2/
Init 1 root txt REG 8,1 150584 654127/sbin/init
UDEVD 415 root 0u CHR 1,3 0t0 6254/dev/null
UDEVD 415 root 1u CHR 1,3 0t0 6254/dev/null
UDEVD 415 Root 2u CHR 1,3 0t0 6254/dev/null
UDEVD 690 Root Mem REG 8,1 51736 302589/lib/x86_64-linux-gnu/libnss_files-2.13.so
SYSLOGD 1246 syslog 2w REG 8,1 10187 245418/var/log/auth.log
SYSLOGD 1246 syslog 3w REG 8,1 10118 245342/var/log/syslog
DD 1271 root 0r REG 0,3 0 4026532038/proc/kmsg
DD 1271 Root 1w FIFO 0,15 0t0 409/run/klogd/kmsg
DD 1271 Root 2u CHR 1,3 0t0 6254/dev/null
Each row displays an open file, and all files opened by all processes are displayed by default if you do not specify a condition.
The meaning of the lsof output column information is as follows:
COMMAND: Name of the process PID: process identifier
USER: Process Owner
FD: File descriptor in which the application recognizes the file through a file descriptor. such as CWD, TXT, etc. type: file type, such as Dir, Reg, etc.
DEVICE: Specifies the name of the disk
Size: Sizes of files
Node: Index node (the identity of the file on disk)
Name: Open the exact name of the file
You may need to know the details of the lsof command yourself
3. Fix too many open files Excepiton (POSIX)
A. The answer to the Internet, especially the use of Baidu, is almost entirely "ulimit-n" view the limit value of the maximum open FD, usually 1024 (meaning that you can open up to 1024), and then
Use "ulimit-n 4096" to increase the limit. Here we come to the conclusion that this solution is a chance-of-luck, detailed and decomposed below.
B. Too many open files appear because:
The 1th section says that all the Linux (POSIX) existence is represented in file form, so the cause of this anomaly (almost) is that you open too many ' files ', exceeding the limit.
That's a big limit in the way it can be used clearly. The following passage gives reasons for not recommending this:
Excerpted from http://oroboro.com/file-handle-leaks-server/
Wrong Answers, Myths and Bad Ideas
Raise the file handle limit
One common answer to this problem are to just raise the limit of open file handles and then restart the server every
Day or every few hours.
This would delay the problem but likely would not fix it. It is possible this your program was not leaking and have a
Legitimate need to hold a large number of file handles. But if your program is designed correctly there usually isn ' t
A need to keep a large number of handles Open–even if you have thousands of simultaneous connections. We ' ll discuss
Some methods of managing that later.
If This is a good idea the operating system would already come configured with a higher file descriptor limit. If
This is necessary, Apache would require you to up this limit before running.
Cause (most of the time)
The problem is almost certainly, which is leaking file handles. That's, handles is being opened
Done with them they is not closed.
Leaked file handles can come from many sources, not just open files. Some Common Sources Are:sockets,pipes,database
Connections,windows Handles,files.
C. How to troubleshoot and repair (focus)
"When not satisfied with the" ulimit-n 4096 "solution, in-depth want to analyze the reason, the search for the analysis method is similar, most and lsof command-related, the following list some;
=. To find the PID for mysqld process, enter:pidof mysqld #pidof命令是找出进程的id号, such as Pidof Java finds the Java process ID number
=. List File opened by a pid:lsof-p ${pid} #-p parameter is--pid meaning, such as Lsof-p 10086 is the print 10086 process for all open files
Or ls/proc/${pid}/fd #和上行一样, view the files that the process opens
=. List File descriptors in Kernel Memory
Sysctl fs.file-nr #结果: Fs.file-nr = 2688 0 379264
= = The number of allocated file handles
= = The number of unused-but-allocated file handles
= = The System-wide maximum number of file handles
Sysctl Fs.file-max #结果是能打开的最大files数量
=. View Files:lsof-u JBoss Open under a user
=. Together with the counter, calculate the number of open files, such as: Lsof-p 10086|wc-l, ls-alt/etc/10086/fd|wc-l, etc.
Most of these analytical methods use lsof Mate parameters and pipeline commands, or a statistical/proc/${pid}/fd directory to analyze the openfiles of your goals.
"Confused
=. Lsof-u root |wc-l The result is 2223, and the ulimit-n result is 1024, why is the root user's current open files still larger than limit? If you don't know that,
Analysis is meaningless, because you are trying to solve the open file limit problem, the result is that Root is currently running with more files than limit.
=. Lsof-p 54552|wc-l The result is 658,54552 is my Java process PID, and ll/proc/54552/fd |wc-l, the result is 358, why statistics the same process is currently
Is there such a big difference in the number of open files? What is the relation between the ulimit-n and the value of the one?
=. First explain 2nd, lsof will also give you memory mapped. So-files-which technically isn ' t the same as a file handle the
Application have control over. /PROC/<PID>/FD is the measuring point for open file descriptors. It means that the results of lsof include
Memory mapped. So-files, these are not principles of general application-controlled FD. The/PROC/<PID>/FD catalogue is a good reflection of the open case of FD.
Properly modified under Lsof:lsof-p <pid> | Grep-v Mem | Egrep-v ' ^command PID ' | Wc-l, this equates to the/proc/<pid>/fd of statistics.
=. is the 2nd question causing the 1th? Using lsof-u root |grep-v mem | Egrep-v ' ^command PID ' | Wc-l, the result is 1221, or greater than limit.
That is, the root user's current open 1221 files is determined, root files limit is 1024 is also determined, and caused more than limit exactly why: actually is
Limit this restriction against the object, and finally find out is this: because the limit is on a per-process base and not per-user. Limit limits are based on
A process (a process owned by that user) is not a user. Ulimit-n is 1024 means that a process executed by the root user can open up to 1024 files at most.
Not a total of 1024 can be opened by root.
=. Sysctl Fs.file-max My result is 379264, which is the number of open files that the Kenel kernel can support in total. This is not going to change.
"Solve
=. At this point, we can explain why the magnification limit is a lucky chance, after all, a process under normal circumstances at the same time to open more than 1024 files or is relatively rare.
=. If you need to analyze leak or view the details of an open fiels, you should start with the PID granularity rather than being confused by the user.
Lsof-p <pid> | Grep-v Mem | Egrep-v ' ^command PID '
or analyze the/PROC/<PID>/FD directory.
=. Uprize limit
Even if you need to expand limit limits, in fact most of the simple "ulimit-n 4096" on the OS does not work, and the operating system is not allowed. The following two ways for reference
Raising the Global Limit. Edit/etc/sysctl.conf and add the following Line:fs.file-max = 65536
Apply the changes With:sudo sysctl-p/etc/sysctl.conf
Raising the Per-user Limit.
&edit as root the following system configuration file:% sudo vi/etc/security/limits.conf
&modify the values for Nuxeo user (we assume here JBOSS was launched with the sytem user "Nuxeo")
Nuxeo Soft Nofile 4096
Nuxeo Hard Nofile 8192
If you want to raise the limits for all users of the can do instead:
* Soft nofile 4096
* Hard Nofile 8192
&edit/etc/pam.d/su:sudo Vi/etc/pam.d/su
&uncomment the line:
Session Required Pam_limits.so
&once You save file, need to logout and login again
D. Summary
Expanding the number of open files limits can work, but before you do, you should be interested in why you are wrong, so you might want to analyze it first.
The above summary of the view is that the online and personal understanding of the proceeds, such as errors, please understand.
Too many open files solutions and principles