Java programs running on Linux may encounter "Too program open files" exceptions, and are common in scenarios such as high-concurrency File System Access and multi-thread network connection.
Files frequently accessed by programs and sockets are file files in Linux. The system must record the name, location, access authority, and other information of each currently accessed file, such an object is called a file entry. "Open files table" (orange in the figure) stores these file entries and is linearly managed as arrays. The file descriptor serves as the pointer from the process to the open files table, that is, the subscript index of the open files table. Each process is associated with the files it accesses.
Each process has a file descriptor table to manage the access of the current process (OpenOrCreate), The file descriptor is associated with the file entry of the file in open files table. Details are not shown. For open files table, how many file entries can be accommodated. In Linux, if the file limit for setting the open files table exceeds the configured value, the system rejects requests for other file operations and throws a too program open files exception. Such restrictions can be divided into system and user levels.
System Level:
System-level settings are valid for all users. You can view the maximum file limit of the system in two ways.
1CAT/proc/sys/fs/file-max
2Sysctl-Check the number of FS. File-Max configurations in the result.
Modify the/etc/sysctl. conf file and configure the fs. File-Max attribute if the attribute does not exist.
UseSysctl-PTo notify the system to enable this configuration.
User level:
Linux limits the number of accessible files for each login user. AvailableUlimit-nTo view the current valid settings. If you want to modify this value, useUlimit-n <setting number>Command.
We recommend that you use the power of 2 to increase the proportion of file descriptors. If the number of file descriptors is 1024, it can be increased to 2048. If not, it can be set to 4096, and so on.
After the too program open files problem occurs, you must first find out the main cause. The biggest possible reason is that the opened file or socket is not properly closed. To locate whether the problem is caused by a Java Process, use the Java Process number to view the file descriptor occupied by the current process:
Java code
- Lsof-p $ attributes of each file descriptor of java_pid
- Lsof-p $ java_pid | Total number of FD in the file descriptor table of the current Java Process WC-l
Analyze the command results to determine whether the problem is caused by abnormal resource release.