Too Many open files

Source: Internet
Author: User

This article transferred from: http://www.cnblogs.com/feiling/archive/2012/12/02/2798098.html

Reference article: http://www.cnblogs.com/challengeof/p/4276659.html

There are too many open files, generally due to improper use of resources by the application, such as not shutting down the socket or database connection in time. However, it is also possible that the application does need to open more file handles, and the system itself limits the number of settings.

Exception 1
Java.net.SocketException:Too Many 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.execInternal (Native Method)
At Java.lang.Runtime.exec (runtime.java:551)
At Java.lang.Runtime.exec (runtime.java:477)
At Java.lang.Runtime.exec (runtime.java:443)

...

The first exception is thrown when an error affects the underlying TCP protocol, while the second exception is thrown when an error affects the I/O operation.

File open Too many worst-case scenarios can cause the system to crash and only restart the server.

Reason:

The maximum number of handles to open files in the operating system is limited, and often occurs when many concurrent users access the server. Because the application server that executes each user needs to load a lot of files (the new one requires a file handle), which results in a lack of open file handles.

Solution:

    • Try to make the class into a jar package, because a jar package consumes only one file handle, and a class consumes a file handle if it is not packaged.
    • Java garbage collection cannot close a file handle opened by a network connection, and if you do not perform close () (For example: Java.net.Socket.close ()), the file handle will persist. and cannot be closed. You can also consider setting the maximum number of open sockets to control this problem.
    • Increase the maximum number of file handles by setting the operating system as appropriate.
      1. Linux
        The source code needs to be modified in Linux kernel 2.4.x, and the kernel will be recompiled before it takes effect. Edit the Include/linux/fs.h file in the source code of the Linux kernel, change the nr_file from 8192 to 65536, and change the nr_reserved_files from 10 to 128. Edit fs/inode.c file to change Max_inode from 16384 to 262144. or edit the /etc/sysctl.conf file by adding two lines Fs.file-max = 65536 and Fs.inode-max = 262144. In general, the maximum number of open files in the system is set to 256 per 4 m of physical memory, such as 256M. You can see the number of open file handles with lsof-p <pid of process>.
      2. Windows
        The maximum file handle is 16,384, and you can see the current number of open handles in the task manager's performance.


Server-side modifications:

View the maximum number of files that the system is allowed to open

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

View the maximum number of files allowed to open per user

Ulimit-a

The discovery system defaults to open files (-N) 1024, which is where the problem occurs.

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

Add content to a file: (* refers to your user name)

* Soft Nofile 65536
* Hard Nofile 65536

Add this line to/etc/pam.d/common-session:

Session Required Pam_limits.so
Otherwise, the configuration on the/etc/security/limits.conf will not take effect.

You can also log out and log in again to make these configurations effective!

Another method:
1. Use Ps-ef |grep Java (Java on behalf of your program, view your program process) to see your process ID, record ID number, assuming the process ID is 12
2. Use: Lsof-p 12 | Wc-l View current file operation status with process ID 12
A file usage of 1052 occurs when the command is executed
3. Use the command: Ulimit-a to see the maximum number of files allowed to open per user
The discovery system defaults to open files (-N) 1024, which is where the problem occurs.
4. Then execute: ulimit-n 4096

Set open files (-N) 1024 to open files (-N) 4096

This increases the maximum number of files that the user is allowed to open

Too Many open files

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.