This site requires JavaScript, please enable it in your browser!
Greenfoot back
Phazon
Phazon wrote ...

2011/4/23

Memory error in exported files

Phazon Phazon

2011/4/23

#
I tried to implement something like a videoplayer in my project. It's a class that grabs pictures out of a folder and changes it's image to that picture, 25 times a second (25fps). Everything worked fine. But, if i export that scenario as a jar-file, the used memory incrases from 67.000kb to 315.000kb (!) within 10(!) seconds. Then, the scenario stops itself and everytime i press start, it instantly stops itself again and again. I dont know why that happens, and i cant use the debug tool, since, when i test it within greenfoots IDE, everthing works fine. I think its because the garbage collection does not delete the images in sutch a short time, but i can not delete them manually either. really dont know what i can do T.T
davmac davmac

2011/4/24

#
When a scenario runs exported, there is an image cache which holds every GreenfootImage that is loaded. This speeds things up a lot for some scenarios. However, images are never freed from the cache which is why you see the problem you do. One way to work around it would be to use Java's own graphics API for loading images. Use GreenfootImage's getAwtImage() method to get a reference to java.awt.image.BufferedImage, and then you can use the documented java methods for BufferedImage. Use the method described here to load the images as an ImageIcon. Use the getImage() method in the ImageIcon to get the Image, then use BufferedImage.getGraphics().drawImage(...) to draw the image onto the GreenfootImage. It's complicated, but it should work.
Phazon Phazon

2011/4/24

#
first of all: thankz. I tried it on that way. First i used an absolute url, just to see whether it works. And, jear, it climbed up to 330.000kb, but at least it didn't crash. But now i'm facing the next problem: the absolute url. I tried to do it on this way:
URL imageUrl;
String imageUrlString = new String("file:/images/");
imageUrlString = imageUrlString.concat(filename); //--->debuger says: "file:/images/Anfangsscreen/intro0000.jpg", which is actually the right url
try {
    imageUrl = new URL(imageUrlString);
 }
catch(MalformedURLException e) {
    imageUrl = null; 
 }
       
 return imageUrl;
That far, it seemed to work, but
URL firstPicUrl = getImagesUrl(0);
Image img = new ImageIcon(firstPicUrl).getImage();
int height = img.getHeight(imgOb);
int width = img.getWidth(imgOb);
using this code, height and width are always -1. "If the height is not yet known, this method returns -1" (quote of the image class-documentation). So i thought "give him time", but it will NEVER know it, even after 30 seconds. Im confused (again) T.T
mik mik

2011/4/24

#
Hi there. First, the "give him time" thought is a good idea. If I remember correctly, the images are loaded asynchronously, so if you check straight away, the image is not completely loaded yet. But as you suspect, if it's still not there after 30 seconds, then you have another problem. First, I think the URL is wrong. I think it should start with "file:///images/...". That because the protocol ends in a double slash ("file://") and if you use an absolute path name, is starts with a slash ("/images/..."). However, using absolute path names is a Bad Idea for a variety of reasons (cannot move the project, cannot run it on another computer, etc.). So try this to load the images:
public Image getImage(String fileName) { 
    URL imageURL = getClass().getClassLoader().getResource(fileName);
    if(imageURL == null) 
        return null; 
    return new ImageIcon(imageURL).getImage();
}
This will use the location of the current class to find the image (so the image must be in the same folder as the class running this code). You could also put them into a subfolder and attach the subfolder name to the URL that is generated here. Michael
Phazon Phazon

2011/4/24

#
thanks a lot it finally worked :) (tried it before, but, somehow it always returned null, so i droped the idea; was standing in the "method described here". Still wondering why it did that^^ )
You need to login to post a reply.