Mt4j and the processing library: clean image resources

I use the mt4j library in a rather large project in which scenes pop out and appear regularly; mt4j uses a processing library for image processing.

We use the loadImage() method to load images, but I do not know how to remove these images from memory when entering a scene; we have problems with memory, because the used memory increases while the application is running.

Do you have an idea?

Thanks Jeremy

EDIT:

I 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 this would help me with memory problems, but I still see an increase in memory every time I load an image. Usage example:

 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: Actually this method works fine :) The problem is solved!

+4
source share
5 answers

EDIT: finally, I tried to delete the images at a time, but it does not clear the memory ... Like there are still some other links that I cannot find ... Any idea to be sure of clearing the memory?

My solution to the problem: 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; } } 
0
source

Or maybe just reuse them? Firstly, image1 = loadImage (oneImage), later image1 = loadImage (anotherImage)?

+2
source

For memory leaks in java, you can look at batch processing. I did not find a great solution for processing other than starting

top -s 5

(on Unix systems) and monitor memory usage until it freezes. I recently worked on a datavis project where I created potentially thousands of blitts (in my case, text images for use in opengl) in an ArrayList. Initially, I recreated these arrays on the fly every time I downloaded data, and this contributed to a memory leak.

In my case, I tried to exclude object references and call the garbage collector. I think this may be due to the fact that the processing calls your void draw () method potentially 60+ times per second, but it seems like it never skipped links.

What I did to fix the problem was to load all the images / create blits only once in the setup () method and only add new elements to the ArrayList when new data appeared.

Before that, I would simply recreate the entire ArrayList on the fly, because it seemed fast enough, but with a long run of this terrible memory leak problem.

Now I just got blit assigned to this link, and not every time I created a new object and an ArrayList. It was much more efficient and did not suffer from the same memory problem.

For example, something like this (runs thousands of times in a loop):

 tempImage = imageArrayList.get(i); // just assigns a reference, does not create a new object 

instead of the following:

 tempImage = loadImage("image.jpg"); // this creates a new object every time it is called 
+2
source

LoadImage returns a PImage object. If you drop these PImage links, then System.gc (), the garbage collector, should remove them. Have you tried this?

+1
source

Do you use 2.x processing? If so, perhaps this thread in the section "Processing the forum" may be the answer. There seems to be a memory leak using images in 2.x. And they also indicate the work that you might want to try, here is an example of a hack from PhiLho in the stream that I pointed out. NTN.

 void draw() { PImage img = createImage(width, height, RGB); image(img, 0, 0); g.removeCache(img);// this is avoiding the leak println(frameCount + " " + g.getCache(img)); } 
+1
source

Source: https://habr.com/ru/post/1438840/


All Articles