"Too many open files" in grails application - how to close open files and streams correctly?

I am developing a rather complex web application that calls several processes that are executed externally, the background process grails and reads / writes from / to several files - all in one controller. Everything was fine until I checked it with many requests in close proximity. When I do this, I get the following java error message in the tomcat catalina log file:

WARNING: Exception executing accept
java.net.SocketException: Too many open files
    at java.net.PlainSocketImpl.socketAccept(Native Method)
    at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:408)
    at java.net.ServerSocket.implAccept(ServerSocket.java:462)
    at java.net.ServerSocket.accept(ServerSocket.java:430)
    at org.apache.jk.common.ChannelSocket.accept(ChannelSocket.java:312)
    at org.apache.jk.common.ChannelSocket.acceptConnections(ChannelSocket.java:666)
    at org.apache.jk.common.ChannelSocket$SocketAcceptor.runIt(ChannelSocket.java:877)
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:690)
    at java.lang.Thread.run(Thread.java:662)

At first, after some happy googeling and reading, I suspected that this might be a "system problem", that is, I need to increase the limit for open files for the user who is running tomcat, but it is not.

Java, Grails Groovy, Google Grails, . , " " ( - ) ( ).

:

1) :

def someFile = new File("/some/file.txt")
someFile << "Some content\n"

2) :

def cmd = "bash some-cmd".execute()
cmd.waitFor()

3) :

def fileContent = new File("/some/file.txt").text

4) :

def URL url = new URL("http://www.some.link");
def URLConnection uc = url.openConnection()
def BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream()))
...
br.close()

, , , BufferedReader InputStream, , br.close() .

: ? ? , ", "?

" " ? IOException: .

grails 1.1.1 ( , , , ), groovy 1.8.0, tomcat 6.0.28, apache 2.2.16 ubuntu 10.10.

" " . , BufferedReader. Java- grails:

def URL url = new URL("http://www.some.link");
def URLConnection uc = url.openConnection()
def BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream()))
try{
    ...
}finally{
    br.close()
}

- > " ", , .

+5
3

, , , BufferedReader InputStream, , br.close() .

... .

groovy/grails, Java , , . ,

InputStream is = new FileInputStream(someFile);

// do some work
...

is.close();

, , (...), . , is.close() , - . ( Java) :

InputStream is = new FileInputStream(someFile);
try {
   // do some work
   ...
} finally {
   is.close();
}

( Java 7):

try (InputStream is = new FileInputStream(someFile)) {
   // do some work
   ...
}
+4

, . .

linux, ulimit, . linux 1024

ulimit -n 2048 // to set the file limit to 2048
0

@Sunil Kumar Sahoo @Stephen C.

:

  • , try/catch/finally -
  • Check ulimit settings when working with UNIX / Linux mailboxes. It really depends on the needs of the application.
0
source

All Articles