原文链接:http://www.javaarch.net/jiagoushi/626.htm
java web应用在执行一段时间之后出现了这么个问题
java.net.SocketException: Too many open files at java.net.PlainSocketImpl.socketAccept(Native Method) at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:384) at java.net.ServerSocket.implAccept(ServerSocket.java:453) at java.net.ServerSocket.accept(ServerSocket.java:421) at org.apache.tomcat.util.net.DefaultServerSocketFactory.acceptSocket(DefaultServerSocketFactory.java:60) at org.apache.tomcat.util.net.JIoEndpoint$Acceptor.run(JIoEndpoint.java:216)
用ulimit -a看看linux打开文件限制
[admin@test ~]# ulimit -a core file size (blocks, -c) 0 data seg size (kbytes, -d) unlimited scheduling priority (-e) 0 file size (blocks, -f) unlimited pending signals (-i) 360448 max locked memory (kbytes, -l) 32 max memory size (kbytes, -m) unlimited open files (-n) 1024 pipe size (512 bytes, -p) 8 POSIX message queues (bytes, -q) 819200 real-time priority (-r) 0 stack size (kbytes, -s) 10240 cpu time (seconds, -t) unlimited max user processes (-u) 360448 virtual memory (kbytes, -v) unlimited file locks (-x) unlimited
用lsof -p [进程ID] 可以看到某ID的打开文件状况。进程ID可能用 ps -ef|grep java列出
[admin@test ~]# lsof -p 1940 COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME java 1940 admin cwd DIR 0,68 4096 385090717 /admin/apache-tomcat-7.0.39/bin java 1940 admin rtd DIR 0,68 4096 384141801 / java 1940 admin txt REG 0,68 47308 385122308 /admin/jdk1.6.0_13/jre/bin/java java 1940 admin mem REG 202,3 385122339 /admin/jdk1.6.0_13/jre/lib/i386/client/libjvm.so (path dev=0,68) java 1940 admin mem REG 202,3 385122308 /admin/jdk1.6.0_13/jre/bin/java (path dev=0,68) java 1940 admin mem REG 202,3 385122425 /admin/jdk1.6.0_13/jre/lib/i386/client/classes.jsa (path dev=0,68) java 1940 admin mem REG 202,3 385122426 /admin/jdk1.6.0_13/jre/lib/charsets.jar (path dev=0,68) java 1940 admin mem REG 202,3 384959153 /lib/libresolv-2.12.so (path dev=0,68) java 1940 admin mem REG 202,3 384959143 /lib/libnss_dns-2.12.so (path dev=0,68) java 1940 admin mem REG 202,3 385122355 /admin/jdk1.6.0_13/jre/lib/i386/libnio.so (path dev=0,68) java 1940 admin mem REG 202,3 385122354 /admin/jdk1.6.0_13/jre/lib/i386/libnet.so (path dev=0,68) java 1940 admin mem REG 202,3 385122325 /admin/jdk1.6.0_13/jre/lib/ext/sunjce_provider.jar (path dev=0,68) java 1940 admin mem REG 202,3 385122326 /admin/jdk1.6.0_13/jre/lib/ext/sunpkcs11.jar (path dev=0,68) java 1940 admin mem REG 202,3 385122392 /admin/jdk1.6.0_13/jre/lib/jce.jar (path dev=0,68) java 1940 admin mem REG 202,3 385122405 /admin/jdk1.6.0_13/jre/lib/resources.jar (path dev=0,68) java 1940 admin mem REG 202,3 385122391 /admin/jdk1.6.0_13/jre/lib/jsse.jar (path dev=0,68) java 1940 admin mem REG 202,3 385090820 /admin/apache-tomcat-7.0.39/lib/tomcat-dbcp.jar (path dev=0,68) java 1940 admin mem REG 202,3 385090824 /admin/apache-tomcat-7.0.39/lib/tomcat-jdbc.jar (path dev=0,68) java 1940 admin mem REG 202,3 385090821 /admin/apache-tomcat-7.0.39/lib/tomcat-i18n-es.jar (path dev=0,68) java 1940 admin mem REG 202,3 385090816 /admin/apache-tomcat-7.0.39/lib/jsp-api.jar (path dev=0,68) java 1940 admin mem REG 202,3 385090817 /admin/apache-tomcat-7.0.39/lib/servlet-api.jar (path dev=0,68) java 1940 admin mem REG 202,3 385090813 /admin/apache-tomcat-7.0.39/lib/el-api.jar (path dev=0,68) java 1940 admin mem REG 202,3 385090811 /admin/apache-tomcat-7.0.39/lib/catalina.jar (path dev=0,68) java 1940 admin mem REG 202,3 385090812 /admin/apache-tomcat-7.0.39/lib/ecj-4.2.1.jar (path dev=0,68) java 1940 admin mem REG 202,3 385090819 /admin/apache-tomcat-7.0.39/lib/tomcat-coyote.jar (path dev=0,68) java 1940 admin mem REG 202,3 385122356 /admin/jdk1.6.0_13/jre/lib/i386/libmanagement.so (path dev=0,68) java 1940 admin mem REG 202,3 385122440 /admin/jdk1.6.0_13/jre/lib/rt.jar (path dev=0,68) java 1940 admin mem REG 202,3 384500332 /usr/lib/locale/locale-archive-rpm (path dev=0,68) java 1940 admin mem REG 202,3 385122427 /admin/jdk1.6.0_13/jre/lib/ext/localedata.jar (path dev=0,68) java 1940 admin mem REG 202,3 385090825 /admin/apache-tomcat-7.0.39/lib/tomcat-util.jar (path dev=0,68) java 1940 admin mem REG 202,3 385090807 /admin/apache-tomcat-7.0.39/lib/annotations-api.jar (path dev=0,68) java 1940 admin mem REG 202,3 385090815 /admin/apache-tomcat-7.0.39/lib/jasper.jar (path dev=0,68) java 1940 admin mem REG 202,3 385090818 /admin/apache-tomcat-7.0.39/lib/tomcat-api.jar (path dev=0,68) java 1940 admin mem REG 202,3 385090809 /admin/apache-tomcat-7.0.39/lib/catalina-ha.jar (path dev=0,68) java 1940 admin mem REG 202,3 385090814 /admin/apache-tomcat-7.0.39/lib/jasper-el.jar (path dev=0,68) java 1940 admin mem REG 202,3 385090810 /admin/apache-tomcat-7.0.39/lib/catalina-tribes.jar (path dev=0,68) java 1940 admin mem REG 202,3 385090823 /admin/apache-tomcat-7.0.39/lib/tomcat-i18n-ja.jar (path dev=0,68) java 1940 admin mem REG 202,3 385090822 /admin/apache-tomcat-7.0.39/lib/tomcat-i18n-fr.jar (path dev=0,68) java 1940 admin mem REG 202,3 385090808 /admin/apache-tomcat-7.0.39/lib/catalina-ant.jar (path dev=0,68) java 1940 admin mem REG 202,3 385090794 /admin/apache-tomcat-7.0.39/bin/tomcat-juli.jar (path dev=0,68) java 1940 admin mem REG 202,3 385090781 /admin/apache-tomcat-7.0.39/bin/commons-daemon.jar (path dev=0,68) java 1940 admin mem REG 202,3 385090776 /admin/apache-tomcat-7.0.39/bin/bootstrap.jar (path dev=0,68) java 1940 admin mem REG 202,3 385122350 /admin/jdk1.6.0_13/jre/lib/i386/libzip.so (path dev=0,68) java 1940 admin mem REG 202,3 385122346 /admin/jdk1.6.0_13/jre/lib/i386/libjava.so (path dev=0,68) java 1940 admin mem REG 202,3 384959101 /lib/libnss_files-2.12.so (path dev=0,68) java 1940 admin mem REG 202,3 385122345 /admin/jdk1.6.0_13/jre/lib/i386/libverify.so (path dev=0,68) java 1940 admin mem REG 202,3 384959147 /lib/libnsl-2.12.so (path dev=0,68) java 1940 admin mem REG 202,3 385222531 /tmp/hsperfdata_admin/1940 (path dev=0,68) java 1940 admin mem REG 202,3 384959158 /lib/librt-2.12.so (path dev=0,68) java 1940 admin mem REG 202,3 384959112 /lib/libm-2.12.so (path dev=0,68) java 1940 admin mem REG 202,3 384959123 /lib/libc-2.12.so (path dev=0,68) java 1940 admin mem REG 202,3 384958823 /lib/libdl-2.12.so (path dev=0,68) java 1940 admin mem REG 202,3 385122348 /admin/jdk1.6.0_13/jre/lib/i386/jli/libjli.so (path dev=0,68) java 1940 admin mem REG 202,3 384959167 /lib/libpthread-2.12.so (path dev=0,68) java 1940 admin mem REG 202,3 385122332 /admin/jdk1.6.0_13/jre/lib/i386/native_threads/libhpi.so (path dev=0,68) java 1940 admin mem REG 202,3 384959214 /lib/ld-2.12.so (path dev=0,68) java 1940 admin 0r CHR 1,3 384926820 /dev/null java 1940 admin 1w REG 0,68 26134081 385090804 /admin/apache-tomcat-7.0.39/logs/catalina.out java 1940 admin 2w REG 0,68 26134081 385090804 /admin/apache-tomcat-7.0.39/logs/catalina.out java 1940 admin 3r REG 0,68 28615 385090776 /admin/apache-tomcat-7.0.39/bin/bootstrap.jar java 1940 admin 4r CHR 1,8 384926869 /dev/random java 1940 admin 5r CHR 1,9 384926888 /dev/urandom java 1940 admin 6r REG 0,68 24281 385090781 /admin/apache-tomcat-7.0.39/bin/commons-daemon.jar java 1940 admin 7r REG 0,68 38161 385090794 /admin/apache-tomcat-7.0.39/bin/tomcat-juli.jar java 1940 admin 8r REG 0,68 51139867 385122440 /admin/jdk1.6.0_13/jre/lib/rt.jar java 1940 admin 9w REG 0,68 23357 385090941 /admin/apache-tomcat-7.0.39/logs/catalina.2013-05-29.log java 1940 admin 10w REG 0,68 6736 385090942 /admin/apache-tomcat-7.0.39/logs/localhost.2013-05-29.log java 1940 admin 11w REG 0,68 0 385090943 /admin/apache-tomcat-7.0.39/logs/manager.2013-05-29.log java 1940 admin 12w REG 0,68 0 385090944 /admin/apache-tomcat-7.0.39/logs/host-manager.2013-05-29.log java 1940 admin 13r REG 0,68 54176 385090808 /admin/apache-tomcat-7.0.39/lib/catalina-ant.jar java 1940 admin 14r REG 0,68 48693 385090822 /admin/apache-tomcat-7.0.39/lib/tomcat-i18n-fr.jar java 1940 admin 15r REG 0,68 51678 385090823 /admin/apache-tomcat-7.0.39/lib/tomcat-i18n-ja.jar java 1940 admin 34r REG 0,68 618621 385122391 /admin/jdk1.6.0_13/jre/lib/jsse.jar java 1940 admin 35r REG 0,68 88256 385122392 /admin/jdk1.6.0_13/jre/lib/jce.jar java 1940 admin 36r REG 0,68 226069 385122326 /admin/jdk1.6.0_13/jre/lib/ext/sunpkcs11.jar java 1940 admin 37r REG 0,68 170239 385122325 /admin/jdk1.6.0_13/jre/lib/ext/sunjce_provider.jar java 1940 admin 38u IPv6 221130420 TCP *:http (LISTEN) java 1940 admin 39u sock 0,5 221130418 can't identify protocol java 1940 admin 40u IPv6 221130421 TCP *:8009 (LISTEN) java 1940 admin 41w REG 0,68 15006 385090940 /admin/apache-tomcat-7.0.39/logs/localhost_access_log.2013-05-29.txt java 1940 admin 42u IPv6 221137418 TCP localhost.localdomain:41285->localhost.localdomain:mysql (ESTABLISHED) java 1940 admin 44u IPv6 221132667 TCP localhost.localdomain:mxi (LISTEN) java 1940 admin 46r REG 0,68 311856 384278348 (deleted) /admin/data/_2pb.tis java 1940 admin 47r REG 0,68 148741 384278362 (deleted) /admin/data/_2pb.frq java 1940 admin 48r REG 0,68 311842 384277396 (deleted) /admin/data/_2pc.tis java 1940 admin 87w REG 0,68 4355 388497409 /logs/run.log java 1940 admin 110r CHR 1,8 384926869 /dev/random java 1940 admin 111r REG 0,68 311856 384278348 (deleted) /admin/data/_2pb.tis java 1940 admin 112u unix 0xffff88045ea3b9c0 221132407 socket
ulimit -n 4096 临时把打开文件数的上限设为了4096
如果要永久生效,可以通过下面的方式设置。
修改/etc/security/limits.conf 添加如下一行:
* - nofile 1006154
修改/etc/pam.d/login添加如下一行
session required /lib/security/pam_limits.so