help Regarding OutOfMemoryError Exception

Hello,

I am trying to write a small program, which downloads all images returned by a google image search. First when I give a query like "tiger" to search, it downloads the images numbering till 684. The download of images count start from 0. My code proceeds till 684 (the 684 images are downloaded and saved on my desktop). But here the OutOfMemoryError is being thrown my JVM

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

But still there are images that have to be saved by the my program.

Can any please help how to overcome this exception.

Thank you,

Chaitanya

[635 byte] By [RKCa] at [2007-11-27 11:22:09]
# 1

Either use less memory or give the VM more at startup with the -Xmx option.

If you're downloading images and saving them to disk, you don't have to hold the whole image in memory at once. Download a little, save it, repeat.

jverda at 2007-7-29 14:53:10 > top of Java-index,Java Essentials,Java Programming...
# 2

Thx a lot for the response. Can you please eloborate the explanation for "downloading the image a bit and then saving it to disk." Below I am providing the code which downloads the image when URL is given.

try{

imageio = ImageIO.read(new URL(allLinks[image]));

image_title = filename_tosave + image + "." + image_setA[image];

img = ImageIO.write(imageio,image_setA[image], new File(image_title)); //intelligent and saves in format it finds.

// image_title = "google_image" + image + ".jpg";

//img = ImageIO.write(imageio,"jpg", new File(image_title)); //saves all formats into only jpg..

}catch(Exception e){

System.out.println("The exception is: " + e + " " +allLinks[image]);

}

if(img){

System.out.println("The image " + image_title + " has been saved to local disk succesfully.");

}

allLinks[image]is an array which contains all URL and the above code runs under a for loop.

Please explain how to change the above code as per your explanation.

Thanks in advance,

Chaitanya

RKCa at 2007-7-29 14:53:10 > top of Java-index,Java Essentials,Java Programming...