Tomcat report java.io.IOException: too many open files

Source: Internet
Author: User
Tags garbage collection

Today background server (Linux) Tomcat app Java.io.IOException:Too many open files
Some of the information on the internet has been recorded as follows:
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.

异常1:12-Jun-2018 01:30:15.340 严重 [http-nio-8080-Acceptor-0] org.apache.tomcat.util.net.NioEndpoint$Acceptor.run Socket accept failed java.io.IOException: 打开的文件过多    at sun.nio.ch.ServerSocketChannelImpl.accept0(Native Method)    at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:422)    at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:250)    at org.apache.tomcat.util.net.NioEndpoint$Acceptor.run(NioEndpoint.java:453)    at java.lang.Thread.run(Thread.java:748)异常二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) 第一个异常则在错误影响到 I/O 操作时抛出,而第二个异常在错误影响到基础 TCP 协议时抛出。

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

Reason:

操作系统的中打开文件的最大句柄数受限所致,常常发生在很多个并发用户访问服务器的时候.因为为了执行每个用户的应用服务器都要加载很多文件(new一个socket就需要一个文件句柄),这就会导致打开文件的句柄的缺乏.

Solve:

1. make the class into a jar package as much as possible, because a jar package consumes only one file handle, and if not packaged, a class consumes a file handle.
2.Java garbage collection cannot shut down 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.
3. make the relevant settings for the operating system and increase the maximum number of file handles.

 a、Linux    在 Linux内核2.4.x中需要修改源代码,然后重新编译内核才生效。    编辑Linux内核源代码中的 include/linux/fs.h文件,将 NR_FILE 由8192改    为65536,将NR_RESERVED_FILES 由10 改为 128。编辑fs/inode.c 文件将    MAX_INODE 由16384改为262144。或者编辑 /etc/sysctl.conf  文件增加两行    fs.file-max = 65536 和 fs.inode-max = 262144 。一般情况下,系统最大打开    文件数比较合理的设置为每4M物理内存256,比如256M.可以用    lsof -p <pid of process>看打开的文件句柄数。  b、Windows       最大文件句柄是16,384,你在任务管理器的性能这一项中可以看到当前打开      的句柄数。

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 to include content in the file:

* soft nofile 65536 * hard nofile 65536修改完成保存,重启服务器

Another method:
1. Use ps -ef |grep java (Java on behalf of your program, view your program process) to view your process ID, record ID number, assuming the process ID is 1305
2. Use: lsof -p 1305 | wc -l View the current file operation status of process ID 1305
A file usage of 1192 occurs when the command is executed
3. Use the command: ulimit -a view 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

Tomcat report java.io.IOException: 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.