Reusing input streams - Running out of memory while reading multiple files
807580May 7 2010 — edited May 15 2010Hello everybody
I am making a program which shows thumbnails of several different image files. I read the original file, create the thumbnail and then I want to use the same inputstream to read a new file rather than creating a new stream every time. The problem now is that I am running out of memory extremely quickly. Basically what I want to do is to create the thumbnail and the delete the original file from memory.
The image is downsampled, and the function returns an ImageIcon, which is used as icon for JLabels.
I have experiemented with mark() and reset() functions, but that did not go well...
Here is what i got so far...
*****************************************************************************************************************************
public ImageIcon getIcon(int imageNumber) {
//Get and print the heap size
long heapSize = Runtime.getRuntime().totalMemory();
System.out.println("Heap Size = " + heapSize);
ImageIcon icon = null;
Image img = null;
try {
//Read the file
//orderImages is a File[] array
InputStream is = new BufferedInputStream(new FileInputStream(orderImages[imageNumber].getAbsolutePath()));
//Set the image file
img = ImageIO.read(is);
//Get the thumbnail
icon = new ImageIcon(img.getScaledInstance(200, -1, 2));
/*
* This is where i want to either delete the input stream, or somehow
* free the memory it uses, because it is not needed anymore for this particular file...
*/
} catch (IOException e) {
}
return (icon);
}
*****************************************************************************************************************************
Any help is apreciated.