Too many open file handles

前端 未结 12 1397
误落风尘
误落风尘 2020-12-16 03:00

I\'m working on a huge legacy Java application, with a lot of handwritten stuff, which nowadays you\'d let a framework handle.

The problem I\'m facing right now is

相关标签:
12条回答
  • 2020-12-16 03:27

    On windows you can look at open file handles using process explorer:

    http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx

    On Solaris you can use "lsof" to monitor the open file handles

    0 讨论(0)
  • 2020-12-16 03:27

    This little script help me to keep eye on count of opened files when I need test ic count. If was used on Linux, so for Solaris you should patch it (may be :) )

    #!/bin/bash
    COUNTER=0
    HOW_MANY=0
    MAX=0
    # do not take care about COUNTER - just flag, shown should we continie or not
    while [ $COUNTER -lt 10 ]; do
        #run until process with passed pid alive
        if [ -r "/proc/$1" ]; then
            # count, how many files we have
            HOW_MANY=`/usr/sbin/lsof -p $1 | wc -l`
            #output for live monitoring
            echo `date +%H:%M:%S` $HOW_MANY
            # uncomment, if you want to save statistics
            #/usr/sbin/lsof -p $1 > ~/autocount/config_lsof_`echo $HOW_MANY`_`date +%H_%M_%S`.txt
    
            # look for max value
            if [ $MAX -lt $HOW_MANY ]; then
                let MAX=$HOW_MANY
                echo new max is $MAX
            fi 
            # test every second. if you don`t need so frequenlty test - increase this value
            sleep 1
        else
            echo max count is $MAX
            echo Process was finished
            let COUNTER=11
        fi
    done
    

    Also you can try to play with jvm ontion -Xverify:none - it should disable jar verification (if most of opened files is jars...). For leaks through not closed FileOutputStream you can use findbug (mentored above) or try to find article how to patch standard java FileOutputStream/FileInputStream , where you can see, who open files, and forgot close them. Unfortunatly, can not find this article right now, but this is existing :) Also think about increasing of filelimit - for up-to-date *nix kernels is not a problem handle more than 1024 fd.

    0 讨论(0)
  • 2020-12-16 03:29

    One good thing I've found for tracking down unclosed file handles is FindBugs:

    http://findbugs.sourceforge.net/

    It checks many things, but one of the most useful is resource open/close operations. It's a static analysis program that runs on your source code and it's also available as an eclipse plugin.

    0 讨论(0)
  • 2020-12-16 03:30

    Its worth bearing in mind that open sockets also consume file handles on Unix systems. So it could very well be something like a database connection pool leak (e.g. open database connections not being closed and returned to the pool) that is leading to this issue - certainly I have seen this error before caused by a connection pool leak.

    0 讨论(0)
  • 2020-12-16 03:36

    Google for an app called filemon from system internals.

    BTW, to track this down you may be able to use something like aspectj to log all calls that open and close files and log where they occur.

    0 讨论(0)
  • 2020-12-16 03:43

    This may not be practical in your case, but what I did once when I had a similar problem with open database connections was override the "open" function with my own. (Conveniently I already had this function because we had written our own connection pooling.) In my function I then added an entry to a table recording the open. I did a stack trace call and saved the identify of the caller, along with the time called and I forget what else. When the connection was released, I deleted the table entry. Then I had a screen where we could dump the list of open entries. You could then look at the time stamp and easily see which connections had been open for unlikely amounts of time, and which functions had done these opens.

    From this we were able to quickly track down the couple of functions that were opening connections and failing to close them.

    If you have lots of open file handles, the odds are that you're failing to close them when you're done somewhere. You say you've checked for proper try/finally blocks, but I'd suspect somewhere in the code you either missed a bad one, or you have a function that hands and never makes it to the finally. I suppose it's also possible that you really are doing proper closes every time you open a file, but you are opening hundreds of files simultaneously. If that's the case, I'm not sure what you can do other than a serious program redesign to manipulate fewer files, or a serious program redesign to queue your file accesses. (At this point I add the usual, "Without knowing the details of your application, etc.)

    0 讨论(0)
提交回复
热议问题