java.net.SocketException: Too many open files 问题的解决办法

来源:互联网 发布:数据采集与无线传输 编辑:程序博客网 时间:2024/06/05 18:52

java.net.SocketException: Too many open files 问题的解决办法

linux 上tomcat 服务器抛出socket异常“文件打开太多”的问题
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:450)
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.PoolTcpEndpoint.acceptSocket(PoolTcpEndpoint.java:407)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:70)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
原本以为是tomcat的配置或是应用本身的问题,"谷歌"一把后才发现,该问题的根本原因是由于系统文件资源的限制导致的。

具体可以参考http://www.bea.com.cn/support_pattern/Too_Many_Open_Files_Pattern.html
的说明。具体的解决方式可以参考一下:
1。ulimit -a 查看系统目前资源限制的设定。
   [root@test security]# umlimit -a
-bash: umlimit: command not found
[root@test security]# ulimit -a
core file size        (blocks, -c) 0
data seg size         (kbytes, -d) unlimited
file size             (blocks, -f) unlimited
max locked memory     (kbytes, -l) unlimited
max memory size       (kbytes, -m) unlimited
open files                    (-n) 1024
pipe size          (512 bytes, -p) 8
stack size            (kbytes, -s) 8192
cpu time             (seconds, -t) unlimited
max user processes            (-u) 7168
virtual memory        (kbytes, -v) unlimited
[root@test security]#
通过以上命令,我们可以看到open files 的最大数为1024
那么我们可以通过一下命令修改该参数的最大值
2. ulimit -n 4096
[root@test security]# ulimit -n 4096
[root@test security]# ulimit -a
core file size        (blocks, -c) 0
data seg size         (kbytes, -d) unlimited
file size             (blocks, -f) unlimited
max locked memory     (kbytes, -l) unlimited
max memory size       (kbytes, -m) unlimited
open files                    (-n) 4096
pipe size          (512 bytes, -p) 8
stack size            (kbytes, -s) 8192
cpu time             (seconds, -t) unlimited
max user processes            (-u) 7168
virtual memory        (kbytes, -v) unlimited

这样我们就修改了系统在同一时间打开文件资源的最大数,基本解决以上问题。

以上部分是查找网络上的解决方法。设置了之后段时间内有作用。


后来仔细想来,问题还是要从根本上解决,于是把以前的代码由认真地看了一遍。终于找到了,罪魁祸首。

在读取文件时,有一些使用的BufferedReader 没有关闭。导致文件一直处于打开状态。造成资源的严重浪费。

修改之后的简单代码如下:


public void test(){
    BufferedReader reader =null;
    try{
        reader = 读取文件;
        String line = "";
        while( ( ine=reader.readLine())!=null){
           其他操作
        }

    } catch (IOException e){
        System.out.println(e);
    } finally{ 
         if(reader !=null){
                try {
                    reader.close();
                } catch (IOException e) {
                      e.printStackTrace();
                }
          }
    }

}

以上转自:http://www.cnitblog.com/rd416/archive/2008/08/07/47724.aspx




更多参考:

AIX mount RedHat的NFS

误删除/dev/dsk 和/dev/rdsk 下的文件怎么办?

unary operator expected

bash: /root/.bash_profile: line 15: syntax error: unexpected end of file

Linux下如何查看文件秒级修改及访问时间

EM乱码解决

linux里端口转发

windows xp 下使用FileZilla密钥

java.net.SocketException: Too many open files 问题的解决办法

UNExcepted inconsistency; run fsck manually

如何修改linux的mac地址?

mysqldump: Got error: 1066: Not unique table/alias

rsync详解一

rsync详解二

更改mysql的默认安装目录

httpd: apr_sockaddr_info_get() failed for centos1113

Real domain name required for sender address

Connection refused by [127.0.0.1]

MySQL bin_log文件占用空间太大

Centos 5 多路径配置步骤

rpc mount export: RPC: Unable to receive; errno = No route to host

nohup和screen的比较

vmware workstation 8 共享磁盘




原创粉丝点击