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 2079 times)
Re: requestImage Memory leak - confused!
Reply #15 - Dec 16th, 2008, 9:56pm
 
can't really help here at the moment - am using 135 and i don't have requestImage. i need to update but it's a 65M download...

we also haven't considered the async nature of things - there'll be a delay between requesting the image and being able to use it - see requestImage description.

but the code should be as simple as taking your first example, changing the requestImage to getPicture and adding the stuff in my post to it (with PhiLho's cast fix in the return).

> also the add function doesn't seem to work

this is curious but could be due to the async thing i mentioned...
Re: requestImage Memory leak - confused!
Reply #16 - Dec 17th, 2008, 1:27am
 
Thanks again. Ok I kept trying and I have it working. Kind of... My guess is though that this is not the solution to my problem. I may have 50 large images in my HashMap cache at the end. That will still mean I run out of ram at the end. My actual aim was to load an image, then dispose of it and free up the ram and so on. How can I do this with loading these 50 images at random?

Thanks for any suggestions


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

void setup() {
}

void draw() {
 if ((bigImage != null)&&(bigImage.width > 0)){
   image(bigImage, 0, 0);
 }
}



void keyPressed() {
 if (key == BACKSPACE) {
   bigImage = getPicture("something.jpg");
 }

 if (key == ENTER) {
   bigImage = getPicture("100_0011.JPG");
 }


}



PImage getPicture(String filename) {
 if (pictures.containsKey(filename)) {
   // already loaded
   println("already exits");
   return (PImage) pictures.get(filename);
 }
 else {
   // load and remember
   PImage image = requestImage(filename);
   pictures.put(filename, image);
   println("sa");
   return image;
 }
}

Re: requestImage Memory leak - confused!
Reply #17 - Jan 2nd, 2009, 2:48am
 
Hi, I have a similar problem, so I discovered this thread.

I too have to load huge amounts of images for my project (possibly thousands with a size of up to 500x500 each).
What I did, was using a LRUCache data structure of a certain size instead of a hash map.

I save the PImage objects in the cache and retrieve them from there. When a cache miss occurs, I reload the image and put it into the cache again. This was however incredibly slow in my first approach, until I used loadBytes() and new PImage(java.awt.Image) instead of loadImage(filename). Don't know why.

This worked acceptably for 0135. Until I decided to update my project to use 1.0.1 - loadImage(java.awt.Image) gives me a ClassCastException (B to I) now. Using the PImage constructor is also strongly discouraged everywhere now. Seems I've got to find another way, so I will give the requestImage method a try.
Re: requestImage Memory leak - confused!
Reply #18 - Jan 26th, 2009, 5:47pm
 
Hello,

Did you finally find a workaround??

I might have the same problem, and I was wondering if you have any new ideas about large images..
Pages: 1 2