loading images in List as ImageIcon causes OutOfMemoryError.
I am trying to figure out why the bit of code below is causing such a memory problem. I have the JRE starting up with 256MB of ram (got 2GB on system). I am loading 200K images at most, and after 3 or so images, I am getting lots of OutOfMemory exceptions. Any ideas?
List images =new ArrayList(50);
File[] files = f.listFiles(new FileFilter(){
publicboolean accept(File fi){
// Only allow .gif, .png, .jpg and .jpeg
String ext = fi.getName().toLowerCase();
if (fi.isFile() && (ext.indexOf(".png") > 0)
|| (ext.indexOf(".gif") > 0)
|| (ext.indexOf(".jpg") > 0)
|| (ext.indexOf(".jpeg") > 0)){
returntrue;
}
returnfalse;
}
});
if (null != files && files.length > 0){
for (int cntr = 0; cntr < files.length; cntr++){
try{
File fil = (File) files[cntr];
System.out.println("Loading " + fil.getAbsolutePath());
ImageIcon ii =new ImageIcon(fil.getAbsolutePath());
images.add(ii);
}catch (Throwable e){
// ignore any per file to image translation errors.
e.printStackTrace(System.out);
}
}
}

