http://www.linuxeye.com/Linux/2781.html

Tomcat报 Jul 21, 2015 8:45:23 AM org.apache.tomcat.util.net.JIoEndpoint$Acceptor runSEVERE: Socket accept failedjava.net.SocketException: Too many open filesat java.net.PlainSocketImpl.socketAccept(Native Method)at java.net.AbstractPlainSoc

Tomcat报

Jul 21, 2015 8:45:23 AM org.apache.tomcat.util.net.JIoEndpoint$Acceptor run
SEVERE: Socket accept failed
java.net.SocketException: Too many open files
at java.net.PlainSocketImpl.socketAccept(Native Method)
at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:398)
at java.net.ServerSocket.implAccept(ServerSocket.java:530)
at java.net.ServerSocket.accept(ServerSocket.java:498)
at org.apache.tomcat.util.net.DefaultServerSocketFactory.acceptSocket(DefaultServerSocketFactory.java:60)
at org.apache.tomcat.util.net.JIoEndpoint$Acceptor.run(JIoEndpoint.java:216)
at java.lang.Thread.run(Thread.java:745)

解决的思路:
一眼异常立刻想到ulimit,于是使用

ulimit -a

查看Linux内核允许的最大资源

open files (-n) 1024

需要调大限制

ulimit -n 65535

经过观察,问题依旧...

通过

lsof|grep tomcat|wc -l

看到tomcat的io居然有1082个,立马想到了nginx与tomcat的不协调造成的,据网络资料显示:tomcat端默认开启了 keepalive,而nginx把连接交给了tomcat并不关系是否关闭,因此tomcat需要等待"connectionTimeout"设置的超 时时间来关闭,所以最好设置“maxKeepAliveRequests”为1,让每个连接只相应一次就关闭,这样就不会等待timeout了。因此设置 tomcat:

<Connector port="8080" protocol="HTTP/1.1"
maxKeepAliveRequests="1"
connectionTimeout="20000"
URIEncoding="UTF-8"
redirectPort="8443" />
<!-- A "Connector" using the shared thread pool-->
<!--
<Connector executor="tomcatThreadPool"
port="8080" protocol="HTTP/1.1"
maxKeepAliveRequests="1"
connectionTimeout="20000"
redirectPort="8443" />

重启tomcat,观察,问题依旧!

翻页查看lsof

lsof|grep tomcat|more

发现有大量的memcached的IO,因此把问题定位在memcached上,为了方便观察,新建memcached实例,发现memcached的IO数量逐步增加,最终导致tomcat崩溃!
嗨,终于找到问题了 ^O^

通过code review,发现每次调用memcached的API,都会new一个XMemcachedClient(一个memcached客户端),每次new的时候都会设置一个尺寸的连接池,而连接是预建立的,因此导致memcached的IO数目剧增。

问题终于解决了,memcached的IO数目稳定了,tomcat也运行良好。。。

不经意间,发现memcached的IO数目以连接池的尺寸在递增,心里一落千丈,拔凉拔凉的,貌似还是没解决!

查看tomcat的日志,发现了端倪:

Jul 21, 2015 3:06:40 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/MobileService] appears to have started a thread named [Xmemcached-Reactor-0] but has failed to stop it. This is very likely to create a memory leak.

原来在每次重新部署webapp时,tomcat杀不掉XmemcachedClient的连接进程,由于使用的是spring,所以需要在memcachedClient的bean里增加"destroy-method"。
OK,到此为止tomcat的IO以及memcached的IO都稳定了,并在合理范围之内,tomcat的是160左右。
为了担心“maxKeepAliveRequests”的设置对tomcat的性能有影响,暂时删除了“ maxKeepAliveRequests”,需要对tomcat的整体性能优化进行了解才去配置。
小插曲:由于使用了spring框架的监听配置Log4j,如果下面的顺序颠倒了会造成不能写日志的问题

<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

经过一天的推理、怀疑、实验、否定,终于搞定,又过了一次"福尔摩斯"的侦探瘾,作此标记,留给后来人!

今天发现打开的IO仍然在增加,虽然幅度比较少,但最高也达到了960+,因此需要优化Tomcat配置,增加线程池,并配置keepAliveTimeout和maxKeepAliveRequests

<Executor name="mobileThreadPool" namePrefix="catalina-exec-"
maxThreads="600" minSpareThreads="20" maxIdleTime="60000" /> <Connector executor="mobileThreadPool" port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
keepAliveTimeout="15000"
maxKeepAliveRequests="1"
URIEncoding="UTF-8"
redirectPort="8443" />

今天使用压力测试,依然出现“Too many open files”,使用ulimit发现“open files (-n)”仍然是1024,那么之前设置的65535是没起作用,而切换到root用户发现却是65535;而在非root用户下

ulimit -n 65535

会报异常:

ulimit: max user processes: cannot modify limit

原来是被 /etc/security/limits.conf 限制了,打开此文件即可看到,对默认用户是有限制的,因此可以加入

*        soft    noproc 65535
* hard noproc 65535
* soft nofile 65535
* hard nofile 65535

这样就非root用户就可以设置ulimit为65535了。

设置了ulimit后,我在root下启动tomcat,压力还会容易出现“Too many open files”,但其实使用“lsof -u root|wc -l”并不多,才3000+;root后来我单独建立了个用户来启动,暂时没出现问题。

PS:

/proc/sys/fs/file-max表示kernel中维护的最大文件描述符数目。不管这个系统上有多少了用户登录,有多少个进程在运行,所有打开的文件数目总合都不能超过这个数字。

/etc/security/limit.conf用来设置每个用户最多可以打开的文件数目。

普通用户可以通过ulimit -n 命令来设置hard limit(只能改小) 和soft limit(可以改大和改小).

把上面的内容概括成3条就是:

  1. /proc/sys/fs/file-max 控制整个系统。
  2. /etc/security/limit.conf控制每个用户, 且受到(1)的限制。
  3. ulimit -n用来控制shell及其子进程, 且受到(2)的限制。

转载请保留固定链接: http://www.linuxeye.com/Linux/2781.html

Tomcat报异常:Too many open files 的解决之路的更多相关文章

  1. 启动Tomcat报异常host-manager does not exist or is not a readable directory

    前几天重新安装了Tomcat6,安装完Tomcat6后在wepapps下面会有一些tomcat自带的项目(root.manager.host- manager等几个),这几天项目没什么用我就删掉了,后 ...

  2. tomcat报异常Invalid character found in method name. HTTP method names must be tokens

    最近监控了一下测试环境的日志,突然出现如下一个异常 由Error parsing HTTP request header可以看出是由于解析请求头出错导致的,但是它属于DEBUG级别的异常,虽然不影响系 ...

  3. eclipse中同步代码PULL报错checkout conflict with files的解决方法

    1.Team--->Synchronize Workspace 2.在同步窗口找到冲突文件,把自己本地修改的复制出来 3.在文件上右键选择 Overwrite----->Yes , 4.再 ...

  4. 记一次Tomcat无法正常启动的查错与解决之路

    使用LombozEclipse运行某Web应用,结果总是404. 换另一个Eclipse运行,还是404. 换Tomcat到更高版本,还是404. 直接启动Tomcat,闪退. 用重定向拦截输出,可惜 ...

  5. eclipse 启动tomcat报Spring错误 Error creating bean with name 'serviceOrderBiz': Injection of autowired dependencies failed

    启动tomcat报异常,提示Sring无法创建serviceOrderBiz(第一行红字),继续看是因为有一个自动注入的字段无法注入ModuleInterfaceBiz(第二行红字),检查servic ...

  6. 启动Tomcat报WEB-INF\lib\j2ee.jar jar not loaded异常的解决办法

    今天加载工程时突然发现Tomcat报: 2010-7-1 12:11:38 org.apache.catalina.loader.WebappClassLoader validateJarFile 信 ...

  7. 启动tomcat后struts框架报异常严重: Exception starting filter struts2 Unable to load configuration.

    启动tomcat后struts框架报异常严重: Exception starting filter struts2 Unable to load configuration. 出现此异常是因为,str ...

  8. 启动tomcat报host-manager does not exist or is not a readable directory异常

    新安装了一个tomcat6,安装完之后在webapps下面会有一些tomcat自带的项目(ROOT.manager.host-manager...) 把这些没用的项目删掉之后,启动tomcat 报如下 ...

  9. 【转】Eclipse下启动tomcat报错:/bin/bootstrap.jar which is referenced by the classpath, does not exist.

    转载地址:http://blog.csdn.net/jnqqls/article/details/8946964 1.错误: 在Eclipse下启动tomcat的时候,报错为:Eclipse下启动to ...

随机推荐

  1. 深度学习标注工具 LabelMe 的使用教程(Windows 版本)

    深度学习标注工具 LabelMe 的使用教程(Windows 版本) 2018-11-21 20:12:53 精灵标注助手:http://www.jinglingbiaozhu.com/ LabelM ...

  2. Windows 用bat脚本带配置启动redis,并用vb脚本使其在后台运行。

    最近,在Windows上用开发PHP程序,需要用到Redis,每天要打开一个运行redis-server.exe的窗口这样比较烦,因为窗口就一直打开着,不能关闭. 所以就想着通过写脚本的方式,让他在后 ...

  3. echarts的axisLabel的文字内容过长的解决办法

    通过查找资料学习,我总结了四种解决的办法,不一定是最好的,但是希望能够帮助到需要的童鞋,同时如果大家有什么更好的方法欢迎指导. 方法一:这种方法就是将文本内容转换为字符串数组,然后再按需求换行,需要每 ...

  4. 这可能是史上最全的css布局教程

    标题严格遵守了新广告法,你再不爽,我也没犯法呀!话不多说,直入正题. 所谓布局,其实包含两个含义:尺寸与定位.也就是说,所有与尺寸和定位相关的属性,都可以用来布局. 大体上,布局中会用到的有:尺寸相关 ...

  5. 20190226_xlVba提取查新标题和关键词

    Sub MainProc() Dim Sht As Worksheet Dim Wb As Workbook Set Wb = Application.ThisWorkbook Set Sht = W ...

  6. Node.js简述

    Node.js是2009年5月由Ryan Dahl 发布的服务器程序. 它封装了Google V8 JavaScript 引擎, 并将其重建为可在服务器上使用. 它旨在提供一种简单的构建可伸缩网络程序 ...

  7. git branch 不显示的原因

    转自:https://blog.csdn.net/qq_39671159/article/details/81261049 必须使用git init命令创建仓库,执行git add . 和git co ...

  8. Linux虚拟机的三种网络连接方式

    Linux虚拟机的三种网络连接方式 虚拟机网络模式 无论是vmware,virtual box,virtual pc等虚拟机软件,一般来说,虚拟机有三种网络模式: 1.桥接 2.NAT 3.Host- ...

  9. Linux进程间通信机制

    Linux支持管道.信号.unix system V三种IPC(Inter-Process-Communication)机制.以下分别对三种机制加以简单介绍. 一.信号机制: 信号又称作软中断,用来通 ...

  10. MapServer Tutorial——MapServer7.2.1教程学习——第一节用例实践:Example1.2 Static Map with Two Layers

    MapServer Tutorial——MapServer7.2.1教程学习——第一节用例实践:Example1.2 Static Map with Two Layers 一.前言 上一篇博客< ...