Java. io. IOException: Too program open files

Source: Internet
Author: User

The background server (Linux) reported java. io. IOException: Too program open files during audio/video conversion.
I checked some information records on the Internet as follows:

Too many files are opened, which is generally caused by improper use of resources by applications, such as failing to close the Socket or database connection in time. However, the application may need to open a large number of file handles, and the system's settings limit this quantity.
Exception 1
Java.net. SocketException: Too program open files

At java.net. PlainSocketImpl. accept (Compiled Code)
At java.net. ServerSocket. implAccept (Compiled Code)
At java.net. ServerSocket. accept (Compiled Code)
At weblogic. t3.srvr. ListenThread. run (Compiled Code)

Exception 2
Java. io. IOException: too many open files

At java. lang. UNIXProcess. forkAndExec (Native Method)
At java. lang. UNIXProcess. (UNIXProcess. java: 54)
At java. lang. UNIXProcess. forkAndExec (Native Method)
At java. lang. UNIXProcess. (UNIXProcess. java: 54)
At java.lang.Runtime.exe cInternal (Native Method)
At java.lang.Runtime.exe c (Runtime. java: 551)
At java.lang.Runtime.exe c (Runtime. java: 477)
At java.lang.Runtime.exe c (Runtime. java: 443)

...

The first exception is thrown when the error affects the basic TCP protocol, and the second exception is thrown when the error affects the I/O operation.

When the number of files opened is too large, the worst case can cause the system to crash. At that time, the server can only be restarted.

Cause:

The maximum number of handles to open files in the operating system is limited, which often occurs when many concurrent users access the server. in order to execute the application server of each user, many files need to be loaded (a file handle is required for a new socket), which leads to a lack of file handles.

Solution:

Try to compress the class into a jar package, because a jar package consumes only one file handle. If it is not packaged, a class consumes one file handle.
Java garbage collection cannot close the file handle opened by the network connection. If close () is not executed (for example: java.net. socket. close. you can also consider setting the maximum number of opened sockets to control this problem.
Set the operating system to increase the maximum number of file handles.
Linux
In Linux kernel 2.4.x, You need to modify the source code and then re-compile the kernel. Edit the include/linux/fs. h file in the linux kernel source code, change NR_FILE from 8192 to 65536, and change NR_RESERVED_FILES from 10 to 128. Edit the fs/inode. c file and change MAX_INODE from 16384 to 262144. Alternatively, add two lines to the/etc/sysctl. conf file: fs. file-max = 65536 and fs. inode-max = 262144. Generally, the maximum number of opened files in the system is set to 256 per 4 MB physical memory, for example, MB. You can use lsof-p <pid of process> to check the number of opened file handles.
Windows
The maximum file handle is 16,384. You can see the number of opened handles in the Task Manager performance item.

Server Side modification:

View the maximum number of files allowed to be opened by the System

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

 

View the maximum number of files that a user can open

Ulimit-

The default value is open files (-n) 1024.

Modify the limit in the system file/etc/security/limits. conf,

Add content to the file:

* Soft nofile 65536
* Hard nofile 65536

 

Other methods:
1. Use ps-ef | grep java (java represents your program, view your program process) to view your process ID and record the ID number. Assume that the process ID is 12.
2. Use: lsof-p 12 | wc-l to view the file operation status with the current process id 12.
When you run this command, the file usage is 1052.
3. Run the command: ulimit-a to view the maximum number of files that each user can open.
The default value is open files (-n) 1024.
4. Run the following command: ulimit-n 4096.
Set open files (-n) 1024 to open files (-n) 4096

This increases the maximum number of files that a user can open.

 

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.