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);

}

}

}

[2280 byte] By [buckman1a] at [2007-10-2 11:00:33]
# 1
Anyone know why loading small images is causing OutOfMemory Errors?
buckman1a at 2007-7-13 3:30:39 > top of Java-index,Desktop,Core GUI APIs...
# 2
It seems like loading images leaks memory somtimes.You can check if it is working by callingImageIcon.getImageLoadStatus()Here is a workaround: http://forum.java.sun.com/thread.jspa?forumID=256&threadID=451560
jvaudrya at 2007-7-13 3:30:39 > top of Java-index,Desktop,Core GUI APIs...
# 3

Thanks for the reply. However, my situation is a little different. I preload up to 100 images first, then rapidly display them on screen like a lottery number. After loading the first 200K image, I get an OutOfMemoryError which is VERY odd to me. I originaly was loading 2MB images and resizing them to smaller size using BufferedImage and such. But that was giving me the problems. So I set Xmx256M, and resized the images in photoshop, and STILL I get memory problems and the app wont run. It is very odd. It's as if anything larger than 64K images can't be loaded using ImageIcon. But I know I've loaded them before as well.

So I guess the thing is, what is the right way to load many images into memory. Maybe ImageIcon is not the right way.

buckman1a at 2007-7-13 3:30:39 > top of Java-index,Desktop,Core GUI APIs...