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

2011/7/12

Error

w1llis w1llis

2011/7/12

#
Every time I open Greenfoot, and it defaults to the game i'm currently working on, i get spammed by the following error:
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
I dont know what it is, but looks like i'm out of memory. I think its because the code is creating 16k objects, is there a way to get around this error other than shortening my object count?
kiarocks kiarocks

2011/7/12

#
whats your code look like
w1llis w1llis

2011/7/12

#
I "fixed" it by just thinning out the spawns. Still would like to know how to fix this though.
w1llis w1llis

2011/7/12

#
            while (random_terrain > 0)  
    {  
        int q = Greenfoot.getRandomNumber(50);
        int w = Greenfoot.getRandomNumber(50);
        addObject(new water(), q, w);  
        random_terrain--;  
    }
        random_terrain = Greenfoot.getRandomNumber(500);
                while (random_terrain > 0)  
    {  
        int q = Greenfoot.getRandomNumber(50);
        int w = Greenfoot.getRandomNumber(50);
        addObject(new water(), -q, -w);  
        random_terrain--;  
    }  
        random_terrain = Greenfoot.getRandomNumber(500);
                while (random_terrain > 0)  
    {  
        int q = Greenfoot.getRandomNumber(50);
        int w = Greenfoot.getRandomNumber(50);
        addObject(new water(), q, -w);  
        random_terrain--;  
    }  
        random_terrain = Greenfoot.getRandomNumber(500);
                while (random_terrain > 0)  
    {  
        int q = Greenfoot.getRandomNumber(50);
        int w = Greenfoot.getRandomNumber(50);
        addObject(new water(), -q, w);  
        random_terrain--;  
    }  

 
            while (random_terrain > 0)  
    {  
        int q = Greenfoot.getRandomNumber(50);
        int w = Greenfoot.getRandomNumber(50);
        addObject(new Sand(), q, w);  
        random_terrain--;  
    }
        random_terrain = Greenfoot.getRandomNumber(500);
                while (random_terrain > 0)  
    {  
        int q = Greenfoot.getRandomNumber(50);
        int w = Greenfoot.getRandomNumber(50);
        addObject(new Sand(), -q, -w);  
        random_terrain--;  
    }  
        random_terrain = Greenfoot.getRandomNumber(500);
                while (random_terrain > 0)  
    {  
        int q = Greenfoot.getRandomNumber(50);
        int w = Greenfoot.getRandomNumber(50);
        addObject(new Sand(), q, -w);  
        random_terrain--;  
    }  
        random_terrain = Greenfoot.getRandomNumber(500);
                while (random_terrain > 0)  
    {  
        int q = Greenfoot.getRandomNumber(50);
        int w = Greenfoot.getRandomNumber(50);
        addObject(new Sand(), -q, w);  
        random_terrain--;  
    }  
    

    
            while (random_terrain > 0)  
    {  
        int q = Greenfoot.getRandomNumber(50);
        int w = Greenfoot.getRandomNumber(50);
        addObject(new grass(), q, w);  
        random_terrain--;  
    }
        random_terrain = Greenfoot.getRandomNumber(500);
                while (random_terrain > 0)  
    {  
        int q = Greenfoot.getRandomNumber(50);
        int w = Greenfoot.getRandomNumber(50);
        addObject(new grass(), -q, -w);  
        random_terrain--;  
    }  
        random_terrain = Greenfoot.getRandomNumber(500);
                while (random_terrain > 0)  
    {  
        int q = Greenfoot.getRandomNumber(50);
        int w = Greenfoot.getRandomNumber(50);
        addObject(new grass(), q, -w);  
        random_terrain--;  
    }  
        random_terrain = Greenfoot.getRandomNumber(500);
                while (random_terrain > 0)  
    {  
        int q = Greenfoot.getRandomNumber(50);
        int w = Greenfoot.getRandomNumber(50);
        addObject(new grass(), -q, w);  
        random_terrain--;  
    }  

Thats just the bit that randomizes the map.
davmac davmac

2011/7/12

#
First, make sure you have the latest Java update. More recent JDK versions have a higher default maximum memory use. Second, if that still doesn't help, you can try editing the "greenfoot.defs" file (part of the Greenfoot installation) and chaning the "bluej.vm.args" setting. The default is: bluej.vm.args=-Xincgc -Dapple.awt.graphics.UseQuartz=true You could try: bluej.vm.args=-Dapple.awt.graphics.UseQuartz=true -Xmx512m I.e.: remove "-Xincgc" and add "-Xmx512m". If you are not on Mac OS you can also safely remove the "-Dapple.awt.graphics.UseQuartz=true". The "-Xmx512m" setting increases the maximum amount of memory that Java will allocate. However, it works only within Greenfoot. If you export your scenario to the gallery, you may find that it won't run. The only solution then is to use less memeory (i.e. create less objects). However, 16000 objects shouldn't really be enough to cause a problem. Are they all actors? If you show the code, then we might be able to point out why it is eating up so much memory.
davmac davmac

2011/7/12

#
You posted just as I was writing. Can you indent the code properly - it makes it much easier to read. Press ctrl+shift+I in the editor to do it automatically (command+shift+I on Mac). There's nothing wrong with the code you posted, except it's not complete. Could you upload the scenario? The problem might be in the way the terrain elements are constructed for example.
w1llis w1llis

2011/7/12

#
Sure, the project is based on the ScrollWorld scenario by grfottoxx. I need to finish up a few things before I upload (general errors) Erm, how do i upload? Nevermind, it worked, gave me an error when i clicked Update...
GameCode GameCode

2011/7/12

#
I get this error sometimes too. But when I close Greenfoot and open my project again it works for some reason.
davmac davmac

2011/7/12

#
w1llis, you have uploaded two different versions. Only one of them has source code. I tried it out and works fine, no problem with OutOfMemoryError etc. What do you have to do to provoke the error?
w1llis w1llis

2011/7/12

#
It works once opened, but when i compile, it gets the error. I also have a 2.5 year old laptop if that could help cause it (which was a low end laptop when i bought it.)
davmac davmac

2011/7/13

#
I can't manage to cause the same error. Probably your laptop has less RAM than mine. Try the things I suggested above.
You need to login to post a reply.