We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › requestImage Memory leak - confused!
Pages: 1 2 
requestImage Memory leak - confused! (Read 2078 times)
requestImage Memory leak - confused!
Dec 15th, 2008, 8:54pm
 
Hi there,

I think I must be doing something very stupid. I wrote a  program that over time has load a large amount of big images. I first used an asynchronous thread method but decided to go over to the new requestImage method instead.
In both cases the memory usage of the program keeps going up and eventually chokes the whole computer. Why are these threads not releasing their memory? Can someone please tell me how to load these images while avoiding this memory leak?

Thanks
Christian

Have a look at this crazy example and watch your memory usage go and up...


PImage bigImage;

void setup() {
}

void draw() {
}

void keyReleased() {
 bigImage = requestImage("something.jpg");
}
Re: requestImage Memory leak - confused!
Reply #1 - Dec 15th, 2008, 10:29pm
 
I can only guess (no time to look at source now), but the process being asynchronous, Processing has to keep an internal reference at the loaded image. I suppose the function was designed to load a limited set of images whose life is equal to the duration of the sketch.
Re: requestImage Memory leak - confused!
Reply #2 - Dec 16th, 2008, 12:25am
 
Ok I am sure you are right but then I have 2 questions:

1 How do I kill a thread and get the ram back?

2 How do I convert this example at the bottom:
http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Integrate;action=display;num=1204990614

Into something that I can call multiple times when I want and decide which single file I want to load in the background?

thanks
Christian
Re: requestImage Memory leak - confused!
Reply #3 - Dec 16th, 2008, 11:32am
 
Well, I finally looked at the code, and saw no problem like the one I "guessed".
The garbage collector should kick in after a while and remove the obsolete images. Unless I am missing something, I am not expert in threads and such.
Re: requestImage Memory leak - confused!
Reply #4 - Dec 16th, 2008, 1:00pm
 
Well,

I am on OSX and I am not seeing the memory usage go down Sad

Can somebody help me with either of these?


1 How do I kill a thread and get the ram back?

2 How do I convert this example at the bottom:
http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Integrate;actio n=display;num=1204990614

Into something that I can call multiple times when I want and decide which single file I want to load in the background?
Re: requestImage Memory leak - confused!
Reply #5 - Dec 16th, 2008, 2:33pm
 
is the processing code based on marius' code he posted?

should be easy enough to add a hashmap containing images keyed on filename and make subsequent requests to the same file return the image already loaded. (or you can, in your code)

but, as has been said, i don't think this was designed for multiple invocations with the same image but for multiple invocations for different images.

(edit, getting marius' name right!)
Re: requestImage Memory leak - confused!
Reply #6 - Dec 16th, 2008, 2:51pm
 
> is the processing code based on marius' code he posted?

code is here

http://dev.processing.org/source/index.cgi/trunk/processing/core/src/processing/core/PApplet.java?rev=5386&view=markup
Re: requestImage Memory leak - confused!
Reply #7 - Dec 16th, 2008, 2:53pm
 
Hi koogs,

Yes that sounds perfect. But how do I convert Marius's code to "add a hashmap containing images keyed on filename and make subsequent requests to the same file return the image already loaded"?

Any help would be appreciated!

I think I am confused about the memory management in Java. How do I get back the memory from old images that I don't need anymore?

Thanks
Re: requestImage Memory leak - confused!
Reply #8 - Dec 16th, 2008, 3:04pm
 
ok, this is without testing (or, indeed, much thought)

Code:

HashMap pictures = new HashMap(); // your cache

PImage getPicture(filename) {
if (picures.containsKey(filename)) {
// already loaded
return pictures.get(filename);
} else {
// load and remember
PImage image = requestImage(filename);
pictures.add(filename, image);
return image;
}
}


delete unwanted stuff from the cache using pictures.remove(filename); (you'll have to remember the filenames, somehow...)

(note to self, capital M in HashMap, small t in HashTable...)
Re: requestImage Memory leak - confused!
Reply #9 - Dec 16th, 2008, 3:29pm
 
Thanks very much for that, but how would I combine this with the asyncloading from Marius?

Will doing pictures.remove(filename) actually clear the memory?

thanks again
Christian
Re: requestImage Memory leak - confused!
Reply #10 - Dec 16th, 2008, 3:46pm
 
the requestImage() call in the middle there is already doing async loading so you don't need marius' code.

the above removes the problem you had in your first post by only loading the image once and returning multiple references to that.

pictures.remove(filename); should, i think, remove the reference to the stored PImage making it suitable for garbage collection. you can force the garbage collector to run there and then (System.gc()) or just leave it to run on its own. it's a bit of a mystery to me being an old c programmer tbh but...
Re: requestImage Memory leak - confused!
Reply #11 - Dec 16th, 2008, 3:50pm
 
thanks very much I will try that!
Re: requestImage Memory leak - confused!
Reply #12 - Dec 16th, 2008, 5:24pm
 
Sorry getting error cannot convert Object to PImage
also the add function doesn't seem to work

-------------

HashMap pictures = new HashMap();  // your cache


PImage getPicture(String filename) {
 if (pictures.containsKey(filename)) {
   // already loaded
   return pictures.get(filename);
 } else {
   // load and remember
   PImage image = requestImage(filename);
   pictures.add(filename, image);
   return image;
 }
}
Re: requestImage Memory leak - confused!
Reply #13 - Dec 16th, 2008, 6:05pm
 
You have to cast the object gotten from the map.
Code:
PImage getPicture(String filename) {
 Object image = pictures.get(filename);
 if (image == null) {
   // Not loaded yet
   image = requestImage(filename);
   pictures.add(filename, image);
 }
 return (PImage) image;
}

[EDIT] Code was wrong, fixed (but still not tested)
Re: requestImage Memory leak - confused!
Reply #14 - Dec 16th, 2008, 6:26pm
 
I am so sorry but I don't seem to understand. I just get errors again. Could you post a fully working sketch?

Thanks again both of you
Pages: 1 2