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

2012/3/14

"java.lang.OutOfMemoryError: Java heap space" greenfoot terminal error

JayWood860 JayWood860

2012/3/14

#
i recieved this error after adding a lot of code in a lot of different classes and there seems to be tons of errors. does this really mean ive written too much code for one scenario, or is there something i can do about this? this is the explanation of the error that came up in the terminal window: java.lang.OutOfMemoryError: Java heap space at java.awt.image.DataBufferInt.<init>(DataBufferInt.java:75) at java.awt.image.Raster.createPackedRaster(Raster.java:470) at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1032) at java.awt.GraphicsConfiguration.createCompatibleImage(GraphicsConfiguration.java:186) at greenfoot.util.GraphicsUtilities.toCompatibleTranslucentImage(GraphicsUtilities.java:285) at greenfoot.util.GraphicsUtilities.loadCompatibleTranslucentImage(GraphicsUtilities.java:227) at greenfoot.GreenfootImage.loadURL(GreenfootImage.java:260) at greenfoot.GreenfootImage.loadFile(GreenfootImage.java:287) at greenfoot.GreenfootImage.<init>(GreenfootImage.java:109) at Asteroids.<init>(Asteroids.java:63) at Rock1.<init>(Rock1.java:12) at Space.addRocks(Space.java:36) at Space.reset(Space.java:57) at Space.<init>(Space.java:23) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:525) at greenfoot.core.Simulation.newInstance(Simulation.java:520) at greenfoot.platforms.ide.WorldHandlerDelegateIDE$3.run(WorldHandlerDelegateIDE.java:406) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:411) at greenfoot.core.Simulation.maybePause(Simulation.java:269) at greenfoot.core.Simulation.runContent(Simulation.java:201) at greenfoot.core.Simulation.run(Simulation.java:194)
kiarocks kiarocks

2012/3/15

#
What is Asteroids.java: line 63?
danpost danpost

2012/3/15

#
That tells you where in your code the execution halted. The '<init>' tells you that it was in the constructor. And, yes, I bet line 63 falls in the Asteriods class constructor. I can not say for certain, but it may be an overload of graphics in your scenario. davmac or one of the other team members would probably be able to shed some light on the issue.
Builderboy2005 Builderboy2005

2012/3/15

#
I think Kiarocks just was asking what was at line 63, as that is what probably was causing the error :] And yeah it sounds like there are too many images being created, either do to the scenario being too large, or because of some infinite loop that was creating images, the only way to tell is to post the code.
JayWood860 JayWood860

2012/3/15

#
i created 8 images in the constructor, and added them later as an increasing variable changed. is this too many?
private GreenfootImage image1 = new GreenfootImage("space.jpg");
        private GreenfootImage image2 = new GreenfootImage("monsters2.jpg");
        private GreenfootImage image3 = new GreenfootImage("colors.jpg");
        private GreenfootImage image4 = new GreenfootImage("rivets.jpg");
        private GreenfootImage image5 = new GreenfootImage("board.jpg");
        private GreenfootImage image6 = new GreenfootImage("Toxic-Sign-Black-Wallpaper.jpg");
        private GreenfootImage image7 = new GreenfootImage("striped wallpaper.jpg");
        private GreenfootImage image8 = new GreenfootImage("distract.jpg");

    public void accelerate()
    {
        Space space = (Space) getWorld();
        Score score = space.scoreCounter();
        int vos = score.getVOS();
        if (vos <= 200)
            {
                myMove(6);
                space.setBackground(image2);
            }
        if (vos > 200 && vos <= 650)
            {
                myMove(7);
                space.setBackground(image5);
            }
        if (vos > 650 && vos <= 800)
            {
                myMove(8);
                space.setBackground(image6);
            }
        if (vos > 800 && vos <= 1000)
            {
                myMove(9);
                space.setBackground(image7);
            }
        if (vos > 1000)
            {
                myMove(12);
                space.setBackground(image8);
            }
        }
JayWood860 JayWood860

2012/3/15

#
JayWood860 wrote...
i created 8 images in the constructor, and added them later as an increasing variable changed. is this too many?
private GreenfootImage image1 = new GreenfootImage("space.jpg");
        private GreenfootImage image2 = new GreenfootImage("monsters2.jpg");
        private GreenfootImage image3 = new GreenfootImage("colors.jpg");
        private GreenfootImage image4 = new GreenfootImage("rivets.jpg");
        private GreenfootImage image5 = new GreenfootImage("board.jpg");
        private GreenfootImage image6 = new GreenfootImage("Toxic-Sign-Black-Wallpaper.jpg");
        private GreenfootImage image7 = new GreenfootImage("striped wallpaper.jpg");
        private GreenfootImage image8 = new GreenfootImage("distract.jpg");

    public void accelerate()
    {
        Space space = (Space) getWorld();
        Score score = space.scoreCounter();
        int vos = score.getVOS();
        if (vos <= 200)
            {
                myMove(6);
                space.setBackground(image2);
            }
        if (vos > 200 && vos <= 650)
            {
                myMove(7);
                space.setBackground(image5);
            }
        if (vos > 650 && vos <= 800)
            {
                myMove(8);
                space.setBackground(image6);
            }
        if (vos > 800 && vos <= 1000)
            {
                myMove(9);
                space.setBackground(image7);
            }
        if (vos > 1000)
            {
                myMove(12);
                space.setBackground(image8);
            }
        }
this is the asteroid class and line 63 is where i created the images(lines 1-8);
mjrb4 mjrb4

2012/3/15

#
How big are the images? If they're all huge then yes, that'd be the source of your problem!
Duta Duta

2012/3/15

#
Also there's another cause that is sometimes the problem: if you compile a scenario lots, eventually you run out of memory and the fix is just to restart Greenfoot
JayWood860 JayWood860

2012/3/15

#
@Duta i just tried to reset it and the terminal window still appeared. @mrjb4 each of these images are 1000x500 cells (size of the world). i just want to change the background of the world as the score(int vos) increases.
danpost danpost

2012/3/15

#
Try NOT saving the images in GreenfootImage variables and just create the images as you need them, like this:
space.setBackground(new GreenfootImage("monsters2.jpg"));
JayWood860 JayWood860

2012/3/15

#
this works!! what is the difference between having image variables and directly placing the images inside the method call?
davmac davmac

2012/3/15

#
If you store all the images in variables, then you have all the images loaded at once. If you only load an image when you need it, you avoid this - you only have one image loaded at a time (garbage collection frees up the memory from the previous image). (For small images, it's probably better to load them beforehand, as loading an image can take a little time; for large images though, that are infrequently used, it's better not to use up your memory).
JayWood860 JayWood860

2012/3/15

#
davmac wrote...
If you store all the images in variables, then you have all the images loaded at once. If you only load an image when you need it, you avoid this - you only have one image loaded at a time (garbage collection frees up the memory from the previous image). (For small images, it's probably better to load them beforehand, as loading an image can take a little time; for large images though, that are infrequently used, it's better not to use up your memory).
alright thanks for the help
ColeoCofer ColeoCofer

2013/5/29

#
You could also cache the images by declaring them static, that way they all share the same image: private static GreenfootImage image1 = new GreenfootImage("space.jpg");
You need to login to post a reply.