I've been experiencing a similar problem to (Too many open file handles) when I try to run a program on a Grid Computer. The option of increasing the operating system limit for the total number of open files on this resource is unavailable.
I tried to catch and handle the exception, but catching the exception does not seem to happen. The exception seems to report itself as a FileNotFoundException
. One of the places the exception is thrown is in the method shown below:
public static void saveImage(BufferedImage bi, String format, File aFile) {
try {
if (bi != null) {
try {
//System.out.println("ImageIO.write(BufferedImage,String,File)");
System.err.println("Not really an error, just a statement to help with debugging");
ImageIO.write(bi, format, aFile);
} catch (FileNotFoundException e) {
System.err.println("Trying to handle " + e.getLocalizedMessage());
System.err.println("Wait for 2 seconds then trying again to saveImage.");
//e.printStackTrace(System.err);
// This can happen because of too many open files.
// Try waiting for 2 seconds and then repeating...
try {
synchronized (bi) {
bi.wait(2000L);
}
} catch (InterruptedException ex) {
Logger.getLogger(Generic_Visualisation.class.getName()).log(Level.SEVERE, null, ex);
}
saveImage(
bi,
format,
aFile);
} finally {
// There is nothing to go in here as ImageIO deals with the stream.
}
}
} catch (IOException e) {
Generic_Log.logger.log(
Generic_Log.Generic_DefaultLogLevel, //Level.ALL,
e.getMessage());
String methodName = "saveImage(BufferedImage,String,File)";
System.err.println(e.getMessage());
System.err.println("Generic_Visualisation." + methodName);
e.printStackTrace(System.err);
System.exit(Generic_ErrorAndExceptionHandler.IOException);
}
}
Here is a snippet from System.err reported one time when the problem occurs:
Not really an error, just a statement to help with debugging
java.io.FileNotFoundException: /data/scratch/lcg/neiss140/home_cream_292126297/CREAM292126297/genesis/GENESIS_DemographicModel/0_99/0/data/Demographics/0_9999/0_99/39/E02002367/E02002367_Population_Male_End_of_Year_Comparison_2002.PNG (Too many open files)
at java.io.RandomAccessFile.open(Native Method)
at java.io.RandomAccessFile.(RandomAccessFile.java:216)
at javax.imageio.stream.FileImageOutputStream.(FileImageOutputStream.java:53)
at com.sun.imageio.spi.FileImageOutputStreamSpi.createOutputStreamInstance(FileImageOutputStreamSpi.java:37)
at javax.imageio.ImageIO.createImageOutputStream(ImageIO.java:393)
at javax.imageio.ImageIO.write(ImageIO.java:1514)
at uk.ac.leeds.ccg.andyt.generic.visualisation.Generic_Visualisation.saveImage(Generic_Visualisation.java:90)
at uk.ac.leeds.ccg.andyt.generic.visualisation.Generic_Visualisation$ImageSaver.run(Generic_Visualisation.java:210)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:439)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
I have some ideas for working around this issue, but does anyone know what is wrong?
(I tried to post a version of this question as an answer to this question, but this was deleted by a moderator.)