• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java.io.IOException: Too many open files

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am receiving this error message after running my program.
Actually the full stack trace is:
java.io.IOException: Too many open files
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:54)
at java.lang.Runtime.execInternal(Native Method)
at java.lang.Runtime.exec(Runtime.java:551)
at java.lang.Runtime.exec(Runtime.java:418)
at java.lang.Runtime.exec(Runtime.java:361)
at java.lang.Runtime.exec(Runtime.java:325)
I am basically doing a UNIX system call (using the JAVA Runtime.exec() method). I need to do this 60,000 time. I am closing my input stream after every time I do the call (i.e. stream.close()). I am getting this after the 11,000 or so system call. I am running this program on a server.
Why am I getting this error after 11,000 or so calls? Thanks.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what are you doing with Runtime.exec() that you cannot do from within Java? I think maybe a little more code or explanation will be needed to help you.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming you do in fact need to use the exec() method of Runtime, you are probably creating too many new processes in succession, without waiting for the existing processes to complete. Try this:
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may need to close all your streams and close the process object. Then null all your values to help the garbage collector.
**************Sample code**************
Process proc = null;
try{
proc = Runtime.getRuntime().exec(cmd);
}catch(IOException x){
//handle error
}
try{
proc.waitFor();
}
catch(InterruptedException x){
}
if(null != proc && null != proc.getInputStream()){
InputStream is = proc.getInputStream();
InputStream es = proc.getErrorStream();
OutputStream os = proc.getOutputStream();
//Do something with the proc or inputstrea
try{
is.close(); is = null;
es.close(); es = null;
os.close(); os = null;
}catch(IOException e){
//handle error
}
}
proc.destroy();
proc = null;
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Mathew,
I am also having the same problem in my project. I used the waitFor() but it didn't solve the problem. Then I used the following code and my problem gets solved.

try the following code



I think this will solve your problem also.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
DOCUMENT ID: 3379-30
SYNOPSIS: Too many open files thrown when Runtime.exec() is invoked
OS: Solaris/SunOS
PLATFORM: Sparc
OS VERSION: 8
TECH AREA: Java
PRODUCT: JDK
KEYWORDS: IOException, Too many open files

DESCRIPTION:
I have encountered the following exception when I run the following program
using Runtime.exec() on Solaris. How do I fix this problem?

// ========= Exception Information ======
java.io.IOException: Too many open files
at java.lang.UNIXProcess.forkAndExec(Native Method) at
java.lang.UNIXProcess.forkAndExec(Compiled Code) at
java.lang.UNIXProcess.<init>(Compiled Code) at
java.lang.Runtime.execInternal(Native Method)
at java.lang.Runtime.execInternal(Compiled Code) at
java.lang.Runtime.exec(Compiled Code)
at java.lang.Runtime.exec(Compiled Code)
at java.lang.Runtime.exec(Compiled Code)
at TestPro.run(Compiled Code)
at TestPro.main(TestPro.java:29)

// ========= My Program ============
public class TestPro{
Runtime runtime = null;
Process p = null;
int exitValue = -1;
public TestPro(){
runtime = Runtime.getRuntime();
};
public void run() {
try {
while (true) {
p = runtime.exec("echo");
java.lang.Thread.sleep(1000);
p.waitFor();

}
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String s[]) {
TestPro tp = new TestPro();
tp.run();
}
}


SOLUTION:

* The subprocess is not killed when there are no more references
* to the <code>Process</code> object, but rather the
subprocess
* continues executing asynchronously.
This implies that a Process object can not be finalized. Also there is nothing
in the specification of waitFor() which implies that it is allowed to close the
I/O streams. The java code could read the process's output after it finishes.
Closing the process file descriptors should be done in the user code.
Add the following lines of code after calling p.waitFor();
p.getInputStream().close();
p.getOutputStream().close();
p.getErrorStream().close();
This should allow you to run the test code indefinitely.
Note that increasing the file descriptor limit in ulimit will only postpone
the occurrence of the problem.


DATE APPROVED: 03/05/01
--------------------------------------------------------------------------------
Copyright (c) 1997 Sun Microsystems, Inc.
 
reply
    Bookmark Topic Watch Topic
  • New Topic