Java's Runtime.exec() openes too many files
I had a stranger problem yesterday at work concerning Java's class java.lang.Runtime. I started processes in a loop on a Linux machine, using Runtime.exec method. After a calling it for a couple hundred times I get java.io.IOException with the message "Too many open files".
According to Mike Noel, what happens is, that Runtime.exec opens 2 java.io.InputStream instances and 1 java.io.OutputStream instance. If you don't close them manually using the java.lang.Process instance returned from Runtime.exec call, they hang forever. As far as I understand the message "Too many open files" is returned cause Linux uses file descriptors for sockets (please correct me if I'm wrong).


5 comments:
Nice tip, although I can't see any info from Sun on runtime.exec() having a 'close' option. How did you close those streams?
Cheers
Steve
Well Runtime.exec returns an instance of java.lang.Process, which has methods to get the InputStream and OutputStream instances so that they can be closed.
see java.lang.Process
Thanks. That did the trick. I found some other documentation about process.destroy() closing all streams too. Apparently there was a problem prior to JRE 1.4 with p.destroy() killing the wrong linux process but that appears to be solved now according to sun's bug-track.
Cheers
Steve
Post a Comment