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

2018/1/29

Heap space after a few level restarts

1
2
3
Paul12345 Paul12345

2018/1/29

#
After restarting the level a few times (from character dying) i get Heap space memory error. Too be fair i scale the background in the world superclass ,but are there any way to fix this ? I scale the image only once in the constructor.
danpost danpost

2018/1/29

#
I may not be an image issue. Show your codes for review (as always).
Paul12345 Paul12345

2018/1/29

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.io.*;
/**
 * Write a description of class LumeScrolling here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LumeScrolling extends World
{

   public static final int lungime = 1500;
   public static final int inaltime = 600;
   
   Actor actorPrincipal;
   Scroller scroller;
    private boolean pause = false,unpause = true,afisat = false,ok = false;
    boolean play = true;
    private static boolean okv0 = true,okv1 = true,okv2 = true;
    private static String r;
    private static int s=Rez();
    private static int shouldAddB =600;
    private boolean afisatB=false;
    Pauza pauza = new Pauza(1500,600);
    Viata0 viata0 = new Viata0(41, 41);
    Viata1 viata1 = new Viata1(41, 41);
    Viata2 viata2 = new Viata2(41, 41);
    public LumeScrolling()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(lungime*Rez()/3,inaltime*Rez()/3, 1,false);
        GreenfootImage bg = new GreenfootImage("cyberpunk-street-dublu.png");
        setBackground(bg);
        bg.scale(lungime*s/3*2,inaltime*s/3);
        int lungBg = bg.getWidth();
        int inalBg = bg.getHeight();
        setPaintOrder(Pauza.class,Viata0.class,Viata1.class,Viata2.class,Tutoriale.class,Intrebari.class,Elev.class,Platforma.class,Cutie.class,PC.class);
        actorPrincipal = new Elev();
        scroller = new Scroller(this, bg,2*lungime*s/3,inaltime*s/3);
        addObject(actorPrincipal ,100,100);
        Greenfoot.setSpeed(49);
        addObject(viata0,lungime*s/3-(59*s/3),37*s/3);
        addObject(viata1,lungime*s/3-(59*s/3)-(50*s/3),37*s/3);
        addObject(viata2,lungime*s/3-(59*s/3)-2*(50*s/3),37*s/3);
        viata0.setOk(okv0);
        viata1.setOk(okv1);
        viata2.setOk(okv2);
        
    }
    
    public void act()
    {
   // if(actorPrincipal!=null)
    scroll();
    String kInput = Greenfoot.getKey();
        okv0 = viata0.getOk();
        okv1 = viata1.getOk();
        okv2 = viata2.getOk();
        if(!okv2)
            play = false;
        if(unpause)
        {
            CheckPause(kInput);
            ok = true;
        }
        else if(pause)
        {
            CheckUnpause(kInput);
            ok = true;
        }
        if(shouldAddB==600&&!afisatB)
        {
            AddBoostere();
            afisatB=true;
        }
        else
        {
            shouldAddB++;
            afisatB=false;
        }
        
    }
    
    public void scroll()
    {
        
    int x = actorPrincipal.getX()-lungime/2;
    int y = actorPrincipal.getY()-inaltime/2;
    scroller.scroll(x,y);
    
    }
    public void CheckPause(String key)
    {
        if(key != null)
        {
            if("p".equals(key))
            {
                if(!afisat)
                {
                    addObject(pauza,1500/2*s/3,600/2*s/3);
                    afisat = true;
                }
                play = false;
                pause = true;
                unpause = false;
            }
        }
    }

    public void CheckUnpause(String key)
    {
        if(key != null)
        {
            if("p".equals(key))
            {
                removeObject(pauza);
                afisat = false;
                play = true;
                pause = false;
                unpause = true;
            }
        }
    }
    
      public void AddBoostere()
    {}
  
    public static void setShouldAddB(int x)
    {
    shouldAddB=x;
    }
        
    public void NivelUrmator()
    {
        
    }
    
    public void RestartNivel()
    {
       
    }
    
    public static void setViata0(boolean x)
    {
        okv0=x;
    }
    
    public static void setViata1(boolean x)
    {
        okv1=x;
    }
    
    public static void setViata2(boolean x)
    {
        okv2=x;
    }
    
    public static boolean getViata0()
    {
        return okv0;
    }
    
    public static boolean getViata1()
    {
        return okv1;
    }
    
    public static boolean getViata2()
    {
        return okv1;
    }
    public static int Rez()
    {
        try{
            FileReader fr = new FileReader("Rezolutie.txt");
            BufferedReader br = new BufferedReader(fr);
            r=br.readLine();
            br.close();
            s = Integer.parseInt(r);
        }
        catch(IOException e)
        {
            s=3;
        }
        return s;
    }
     public static int getRez()
    {
    return s;
    }
    
}
This is my world superclass , following your scrolling tutorial . When i get the heap space error it points to this line exactly
bg.scale(lungime*s/3*2,inaltime*s/3);
And i can start my scenario only if i close greenfoot and open it again.
Paul12345 Paul12345

2018/1/29

#
In my level subclass i only add 2 platforms and i don't modify the background so i'm guessing it isn't because of too many objects.
Paul12345 Paul12345

2018/1/29

#
Background picture resolution is 1216 x 192
danpost danpost

2018/1/29

#
Yeah -- it is most probably an image issue. Your viewable world is awfully large (about twice as wide as I would probably go). Then,, your scrolling background image is a monster. I mentioned in the tutorial (and also in the description of the Scroller class) how large images can cause issues -- to keep image sizes and world viewport to nominal size. Even if you reduce them enough to rid yourself of the java heap space issue, you will still have massive lag issues (you would need to reduce them even more). Also, why you continually call the 'Rez' method. Once you call it and store the value from the file in 's', you have it in 's'. There is no need to go back to the file again.
Paul12345 Paul12345

2018/1/30

#
Ok .Thanks for the advice.
Paul12345 Paul12345

2018/1/30

#
Update : I tried using a smaller world which increased the number of level restarts it takes to fill up heap space and resized my image in photoshop so i don't have to scale it in greenfoot but the problem still occurs. Could it be also because when testing for this problem i restart the world multiple times in a short interval and some objects stay loaded in memory ? (I don't really know how garbage collector works)
danpost danpost

2018/1/30

#
What static content do you have in other classes? Is this LumeScrolling world the only subclass of World in your project and, if not, how do you transition to any other worlds?
danpost danpost

2018/1/30

#
Paul12345 wrote...
Could it be also because when testing for this problem i restart the world multiple times in a short interval and some objects stay loaded in memory ?
The only object I see that stays is the String object named 'r'. Unless it is added to extensively without reducing in length, I do not see it being a problem. However, it does remain and I do not know how that would affect the project when restarted with characters already in the string. Also, your non final static content should be initialized by the world constructor so that their values are reset. Their altered values may cause unwanted behavior from your project on restarts.
Paul12345 Paul12345

2018/1/30

#
danpost wrote...
how do you transition to any other worlds?
the transition between levels looks something like this (also your suggestion)
    public void RestartNivel()
    {
        Greenfoot.setWorld(new Nivel0());
    }
    
    public void NivelUrmator()
    {
        Greenfoot.setWorld(new Nivel1());
    }
In the string i only keep 1 character at the moment and overall over my classes i only have a few static booleans and my character speed which is double type
danpost danpost

2018/1/30

#
Please provide the code to one of the Nivel classes for examination.
Paul12345 Paul12345

2018/1/30

#
And also the scenario runs out of memory each time after an exact amount of restarts . Does this indicate the fact that some things are added to the memory when i create the world but are not removed when the world restarts?
Paul12345 Paul12345

2018/1/30

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Nivel0 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Nivel0 extends LumeScrolling
{

    /**
     * Constructor for objects of class Nivel0.
     * 
     */
    private int s;
    public Nivel0()
    {
       LumeScrolling.setShouldAddB(600);
        prepare();
       
    }
 
 
    private void prepare()
    {
       s=LumeScrolling.getRez();
        Platforma platforma = new Platforma(300);
        addObject(platforma,376,522);
        platforma.setLocation(398*s/3,535*s/3);
       AI ai = new AI();
        addObject(ai,659,525);
        Platforma platforma2 = new Platforma(300);
        addObject(platforma2,895,471);
        platforma2.setLocation(903,426);
        Cutie cutie = new Cutie(40, 40);
        addObject(cutie,395,337);
        platforma2.setLocation(745,514);
        Cutie cutie2 = new Cutie(40, 40);
        addObject(cutie2,349*2/3,343*2/3);
        removeObject(cutie);
        platforma2.setLocation(924,459);
        platforma2.setLocation(924,448);
        ViataPickup viatapickup = new ViataPickup(20, 20);
    addObject(viatapickup,609*s/3,550*s/3);
    Tutorial0 tutorial0 = new Tutorial0(30, 30);
    addObject(tutorial0,403*s/3,498*s/3);
    }
    public void AddBoostere()
    {
    
    JumpBoost jumpboost = new JumpBoost(20, 20);
    addObject(jumpboost,659*s/3-scroller.getScrolledX(),550*s/3);
    }
    public void RestartNivel()
    {
        Greenfoot.setWorld(new Nivel0());
    }
    
    public void NivelUrmator()
    {
        Greenfoot.setWorld(new Nivel1());
    }
}
danpost danpost

2018/1/30

#
Paul12345 wrote...
And also the scenario runs out of memory each time after an exact amount of restarts . Does this indicate the fact that some things are added to the memory when i create the world but are not removed when the world restarts?
Apparently. But what it may be is what I am looking for. If not in one of the Nivel classes, might have to start looking at Actor subclasses.
There are more replies on the next page.
1
2
3