How to get the garbage collector to work?

Hi,

i have i program where i load an image scale it down and save the scaled version in an array. I do this for a whole directory of images.

After every image i set the temporary variable for the loaded image = null and call the function System.gc().

My problem is, that the garbage collector doesnt give the memory of the loaded image free and the used memory of my program grows with every loaded image.

/* Reads all images from a folder an stores them in an Array of images */

publicstatic BufferedImage[] readScaledImagesFromFolder(String folder,

int maxSize){

File dir =new File(folder);/* Open the Folder */

String[] children = dir.list();/* Get the children of the folder */

if (children ==null){

// Either dir does not exist or is not a directory

System.out.println("No images in the folder!");

returnnull;

}else{

/* Init array for images */

BufferedImage[] images =new BufferedImage[children.length];

int i = 0;

int index = 0;

BufferedImage temp;

String filename, fileending;

for (i=0; i<children.length; i++){

// Get filename of file or directory

filename = children[i];

/* Get the fileending of the file */

fileending = filename.toLowerCase().substring(filename.length()-4);

if(fileending.equals(".jpg") || fileending.equals(".bmp")

|| fileending.equals(".png") || fileending.equals(".gif"))

{

/* Read the image */

temp = util.ImageUtils.loadBufferedImage(folder+"/"+filename);

/* Scale the image down and save it in an array */

images[index] = Util.getScaledImage(temp,maxSize);

index++;

}

temp =null;

System.gc();

}

Mosaic.sourceImageNum = index;

System.out.println((index+1)+" resized pictures loaded from folder: "+folder);

return images;

}

}

How can i get the gargabe collector to work after every iteration?

I tried to let the Thread.sleep(10) after System.gc() but it doesnt help.

Thank you every much

JackNeil>

[3365 byte] By [JackNeila] at [2007-11-27 10:24:09]
# 1

System.gc() is merely a suggestion to the garbage collector to run.

You CANNOT force the garbage collector to run if it doesn't want to.

No offence please but the garbage collector is smarter than you will ever be at managing available memory.

maple_shafta at 2007-7-28 17:26:12 > top of Java-index,Java Essentials,Java Programming...
# 2

The garbage collector doesn't need any help here. It never does.

The memory probably will grow, until there's a shortage, then the GC will do it's job.

malcolmmca at 2007-7-28 17:26:12 > top of Java-index,Java Essentials,Java Programming...
# 3

Hm yes.. i now that System.gc() is only a suggestion.

But i know what my program is doing and that it doesnt need the temporary image anymore after i have a scaled down version.

It would be smarter to load the new image over the old temporary image and not to expand the heapsize to maximum.

JackNeila at 2007-7-28 17:26:12 > top of Java-index,Java Essentials,Java Programming...
# 4

> Hm yes.. i now that System.gc() is only a

> suggestion.

> But i know what my program is doing and that it

> doesnt need the temporary image anymore after i have

> a scaled down version.

And the temporay image will become unreachable as soon as reading the next one overwrites your temp variable. Setting the variable to null doesn't have much effect.

> It would be smarter to load the new image over the

> old temporary image and not to expand the heapsize to

> maximum.

Then look at the possibitly of loading the next image into the same bufferedimage.

malcolmmca at 2007-7-28 17:26:12 > top of Java-index,Java Essentials,Java Programming...
# 5

Thank you. :-)

Overloading the same bufferedimage helped. :-)

JackNeila at 2007-7-28 17:26:12 > top of Java-index,Java Essentials,Java Programming...
# 6

Refer java Language Specification

Message was edited by:

krish1315

krish1315a at 2007-7-28 17:26:12 > top of Java-index,Java Essentials,Java Programming...
# 7

And make sure you remove System.gc() from your code. In over a decade of Java programming I don't think I've ever used it or seen a need for using it.

SoulTech2012a at 2007-7-28 17:26:12 > top of Java-index,Java Essentials,Java Programming...
# 8

> Refer java Language Specification

What a completely useless post.

jverda at 2007-7-28 17:26:12 > top of Java-index,Java Essentials,Java Programming...