I’m using mt4j library in a quite big project, in which scenes are pushed and poped regularly ; mt4j uses Processing library to deal with images.
We use the loadImage() method to load images, but I don’t know how to delete these images from memory when a scene is poped ; we have actually memory problems, because the memory used increases during the application running time..
Do you have some idea ?
Thanks
Jérémy
EDIT :
I just wrote an ImageManager class :
package managers;
import java.util.HashMap;
import java.util.Map;
import org.mt4j.MTApplication;
import processing.core.PImage;
public class ImageManager {
private static ImageManager mImageManager = null;
protected MTApplication mApp;
protected Map< String, PImage > mImages;
protected PImage mTempImg;
private ImageManager( MTApplication app ) {
mApp = app;
mImages = new HashMap< String, PImage >();
}
public static synchronized ImageManager getInstance( MTApplication app ) {
if( mImageManager == null ) {
mImageManager = new ImageManager( app );
}
return mImageManager;
}
/**
* Load or retrieve img in memory
*
* @param path Path to the image
* @return <PImage> the image
*/
public PImage getImage( String path ) {
// Search for image
if( mImages.containsKey( path ) ) {
System.out.println( "ImageManager::getImage : image found !" );
mTempImg = mImages.get( path );
}
else {
System.out.println( "ImageManager::getImage : image not found, loading" );
mTempImg = mApp.loadImage( path );
mImages.put( path, mTempImg );
}
return mTempImg;
}
}
Here is my problem : I thought that it would help me with memory problems, but I still see memory increase every time I load an image. An exemple of use :
ImageManager imgManager = ImageManager.getInstance( (MTApplication) app );
PImage image = imgManager.getImage( getPathToIcons() + imagesNames[i] );
//PImage image = app.loadImage(getPathToIcons() + imagesNames[i]);
mSceneImages.add( image );
Any idea ? Thanks
EDIT 2 : In fact, this method is working great 🙂 Problem solved !
EDIT : finally I’ve tryed to delete images at a time, but it doesn’t clean memory… It’s like there still is some other references that I can’t find… Any idea to be sure of memory cleaning ?
My solution to the problem : ImageManager class