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>

