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

2020/12/18

Heap space

Roshan123 Roshan123

2020/12/18

#
Plz tell me all the ways to reduce heap space
nccb nccb

2020/12/18

#
There's a few ways mentioned here in this topic It's usually either making too many actors (e.g. 10000+) or having very large image textures. There's two ways you usually run out of heap space: a memory leak that keeps running out no matter how high the limit, or just wanting to use a bit more memory than Greenfoot's default limit. Greenfoot's memory limit is maybe a bit low so see the suggestion there to just increase the limit to say several gigabytes. If you still run out, there's probably a problem in your code.
Roshan123 Roshan123

2020/12/21

#
plz help me sometimes it shows errors from line 16-20 and sometimes it doesn't show any line It only shows 'heap space exception'
import greenfoot.*;  
public class MyWorld extends World
{
    Counter counter=new Counter();
    public Tank mainTank=new Tank(counter);
    public B mainB=new B(counter,mainTank);
    ScoreBoard sb=new ScoreBoard(460, 340);
    Png p=new Png();
    EnemyTank enemy;
    int tank,b,crate,bottimer,bottimer1;
    int r[]={695,105};
    int r2[]={105,495};
    Color c=new Color(17,17,17,130);
    byte solo=0,duo=0;
    boolean upKeyDown;
    GreenfootImage img=new GreenfootImage(153,103);
    GreenfootImage img1=new GreenfootImage(153,103);
    GreenfootImage img2=new GreenfootImage(303,103);
    GreenfootImage img3=new GreenfootImage(153,106);
    GreenfootImage img4=new GreenfootImage(303,106);
    GreenfootImage bg=new GreenfootImage("soil.png");
    short count=1,cc=1;
    public MyWorld()
    {    
        super(800, 600, 1,true);
        setPaintOrder(ScoreBoard.class,Crate.class,EnemyCannon2.class,EnemyTank.class,EnemyCannon.class,
            EnemyTank1.class,BSheild.class,Sheild.class,Tank.class,B.class,
            Projectile.class,Projectile1.class,Magn_feild.class,Stone.class,Counter.class);
        Greenfoot.start();
        img.setColor(Color.CYAN);
        img.drawRect(50, 50, 101, 51);
        img.setColor(Color.BLACK);
        img.fillRect(51, 51, 100, 50);
        getBackground().drawImage(img, 302, 150);
        addObject(new Label(" DUO ",30), 403, 228);

        img1.setColor(Color.CYAN);
        img1.drawRect(50, 50, 101, 51);
        img1.setColor(Color.BLACK);
        img1.fillRect(51,51, 100, 50);
        getBackground().drawImage(img1, 303, 50);
        addObject(new Label(" SOLO ",30), 402, 128);
        img2.setColor(Color.CYAN);
        img2.drawRect(50, 50, 187, 51);
        img2.setColor(Color.BLACK);
        img2.fillRect(51,51, 186, 50);
        getBackground().drawImage(img2, 269, 250);
        addObject(new Label(" FEEBDBACK ",30), 413, 328);
    }
    public Tank getTank()
    {
       return mainTank;   
    }
    public String getName()
    {
        UserInfo myData = UserInfo.getMyInfo();
        if (myData == null)
            return "Anonymous";
        return myData.getUserName();
    }
    public void gameOver()
    {
        if(UserInfo.isStorageAvailable()) 
        {
            UserInfo myData = UserInfo.getMyInfo();
            if (myData != null) {
                int newValue = counter.getScore();
                if (newValue > myData.getScore()) {
                    myData.setScore (newValue);
                    myData.store();
                }
            }
        } 
        Greenfoot.setWorld(new MyWorld());
    }
    public void act()
    {

        if(solo==0 && duo ==0)
        {
            img3.setColor(new Color(Greenfoot.getRandomNumber(255)+1,Greenfoot.getRandomNumber(255),Greenfoot.getRandomNumber(255)));
            img3.fillRect(48,48, 103, 56);
            img3.setColor(Color.BLACK);
            img3.fillRect(51,51, 97, 50);
            getBackground().drawImage(img3, 302, 350);
            addObject(new Label(" HELP ",30), 400, 428);

            img4.setColor(new Color(Greenfoot.getRandomNumber(255)+1,Greenfoot.getRandomNumber(255),Greenfoot.getRandomNumber(255)));
            img4.fillRect(48, 48, 204, 57);
            img4.setColor(Color.BLACK);
            img4.fillRect(51,51, 197, 50);
            getBackground().drawImage(img4, 272, 450);
            addObject(new Label(" HIGH SCORES ",30), 421, 528);
        }
        Greenfoot.setSpeed(49);
        MouseInfo mouse=Greenfoot.getMouseInfo();
        if(Greenfoot.mousePressed(null))
        {
            int x = mouse.getX();  
            int y = mouse.getY();  
            if(solo==0 && duo == 0 && x>317 && x<507 && y>500 && y<550)
            {
                count+=1;
            }
            if(count%2==0){
                addObject(sb, getWidth() / 2, getHeight() / 2);
            }else removeObject(sb);
            if(solo==0 && duo == 0 && x>350 && x<450 && y>400 && y<450)
            {
                cc+=1;
            }
            if(cc%2==0)
            {
                addObject(p, 160,300);
            }else removeObject(p);
            if(solo==0 && duo == 0 && x>327 && x<505 && y>300 && y<350)
            {
              String col;
              UserInfo me = UserInfo.getMyInfo();
              if(me!=null)
              {
                do
                {
                    col = Greenfoot.ask("send FEEDBACK (max: 50 letters)");
                }while(col.length()>50);
                if (UserInfo.isStorageAvailable())
                {
                    if (col != null && !"".equals(col)) me.setString(1, col);
                    me.store();
                }
              }else addObject(new Label(" Login for \nFeedback ",50), 150, 300);
            }
            else  if(solo==0 && duo == 0 && x>350 && x<451 && y>200 && y<250)
            {
                bg.scale(150, 150);
                setBackground(bg);
                removeObjects(getObjects(Label.class));
                addObject(counter, 410, 15);
                mainTank=new Tank(counter);
                addObject(mainTank, 695,105);
                mainB=new B(counter,mainTank);
                addObject(mainB, 105,495);

                addObject(new EnemyTank(true,3), 695, 495);
                addObject(new EnemyCannon2(20,125), 695, 495);
                addObject(new EnemyTank1(true,3), 105, 105);
                addObject(new EnemyCannon(20,125), 105, 105);
                addObject(new Crate(), 30,30);
                addObject(new Crate(), 770,570);
                addObject(new Speed(), 627, 520);
                addObject(new Speed(), 175, 80);
                addObject(new Magn_feild(), 715, 420);
                addObject(new Magn_feild(), 85, 180);
                addObject(new Stone(), 300,300);
                addObject(new Stone(), 274,300);
                addObject(new Stone(), 248,300);
                addObject(new Stone(), 222,300);
                addObject(new Stone(), 196,353);
                addObject(new Stone(), 196,380);
                addObject(new Stone(), 196,326);
                addObject(new Stone(), 196,406);
                addObject(new Stone(), 604,300);
                addObject(new Stone(), 196,300);
                addObject(new Stone(), 500,300);
                addObject(new Stone(), 526,300);
                addObject(new Stone(), 552,300);
                addObject(new Stone(), 578,300);
                addObject(new Stone(), 604,248);
                addObject(new Stone(), 604,274);
                addObject(new Stone(), 604,222);
                addObject(new Stone(), 604,196);
                solo++;
            }else if (duo==0 && solo == 0 &&  x>350 && x<450 && y>100 && y<150)
            {
                GreenfootImage bg=new GreenfootImage("soil.png");
                removeObjects(getObjects(Label.class));
                bg.scale(150, 150);
                setBackground(bg);
                addObject(counter, 410, 15);
                mainB=new B(counter,mainTank);
                addObject(mainB, 105,495);
                addObject(new EnemyTank(true,3), 695, 105);
                addObject(new EnemyCannon2(20,79), 695, 105);
                addObject(new Stone(), 300,300);
                addObject(new Stone(), 274,300);
                addObject(new Stone(), 248,300);
                addObject(new Stone(), 222,300);
                addObject(new Stone(), 196,353);
                addObject(new Stone(), 196,380);
                addObject(new Stone(), 196,326);
                addObject(new Stone(), 196,406);
                addObject(new Stone(), 604,300);
                addObject(new Stone(), 196,300);
                addObject(new Stone(), 500,300);
                addObject(new Stone(), 526,300);
                addObject(new Stone(), 552,300);
                addObject(new Stone(), 578,300);
                addObject(new Stone(), 604,248);
                addObject(new Stone(), 604,274);
                addObject(new Stone(), 604,222);
                addObject(new Stone(), 604,196);
                duo++;
            }
        }
        if(solo==1)
        {
            int rand=Greenfoot.getRandomNumber(r.length);
            int rand2=Greenfoot.getRandomNumber(r2.length);
            if(tank>0 && --tank ==0) addObject(new Tank(counter),r[rand],r2[rand2]);
            if(tank==0 && getObjects(Tank.class).isEmpty())tank =60;

            if(b>0 && --b ==0) addObject(new B(counter,mainTank),r[rand],r2[rand2]);
            if(b==0 && getObjects(B.class).isEmpty())b =60;

            if(crate>0 && --crate ==0) {addObject(new Crate(),30,30); addObject(new Crate(),770,570);}
            if(crate==0 && getObjects(Crate.class).isEmpty())crate =150;

            if(bottimer>0 && --bottimer ==0) addObject(new EnemyTank(true,3),  695, 495);
            if(bottimer==0 && getObjects(EnemyTank.class).isEmpty())bottimer =100;

            if(bottimer>0 && --bottimer ==0) addObject(new EnemyCannon2(20,125),  695, 495);
            if(bottimer==0 && getObjects(EnemyCannon2.class).isEmpty())bottimer =10;

            if(bottimer1>0 && --bottimer1 ==0) addObject(new EnemyTank1(true,3), 105, 105);
            if(bottimer1==0 && getObjects(EnemyTank1.class).isEmpty())bottimer1 =100;

            if(bottimer1>0 && --bottimer1 ==0) addObject(new EnemyCannon(20,125), 105, 105);
            if(bottimer1==0 && getObjects(EnemyCannon.class).isEmpty())bottimer1 =10;
        }
        if(duo==1)
        {
            int rand=Greenfoot.getRandomNumber(r.length);
            int rand2=Greenfoot.getRandomNumber(r2.length);
            if(b>0 && --b ==0) addObject(new B(counter,mainTank),r[rand],r2[rand2]);
            if(b==0 && getObjects(B.class).isEmpty())b =60; 
            if(bottimer>0 && --bottimer ==0) addObject(new EnemyTank(true,4),  r[rand], r2[rand2]);
            if(bottimer==0 && getObjects(EnemyTank.class).isEmpty())bottimer =100;
            if(bottimer>0 && --bottimer ==0) addObject(new EnemyCannon2(20,77),  0, 0);
            if(bottimer==0 && getObjects(EnemyCannon2.class).isEmpty())bottimer =10;
        }
    }
}
Roshan123 Roshan123

2020/12/22

#
Can anybody plz help me to fix the heap space error
danpost danpost

2020/12/22

#
Roshan123 wrote...
Can anybody plz help me to fix the heap space error
There is no need to keep the 6 images (lines 16 thru 21) referenced by your MyWorld object. Also, you know solo and duo are both zero to start, so I question the location of the code currently between lines 79 and 95 (also noting that processing of this code repeatedly is pointless). First, move line 21 to before line 135. Then, remove lines 16 thru 20. Also, remove lines 79 thru 95 from act method. Now, replace your MyWorld constructor (lines 23 thru 49) with the following:
public MyWorld()
{    
    super(800, 600, 1);
    setPaintOrder(ScoreBoard.class,Crate.class,EnemyCannon2.class,EnemyTank.class,EnemyCannon.class,
        EnemyTank1.class,BSheild.class,Sheild.class,Tank.class,B.class,
        Projectile.class,Projectile1.class,Magn_feild.class,Stone.class,Counter.class);
    Greenfoot.setSpeed(49);
    Greenfoot.start();
    
    GreenfootImage img = new GreenfootImage(153, 103);
    img.setColor(Color.CYAN);
    img.drawRect(50, 50, 101, 51);
    img.setColor(Color.BLACK);
    img.fillRect(51, 51, 100, 50);
    getBackground().drawImage(img, 302, 150);
    addObject(new Label(" DUO ",30), 403, 228);
    
    img = new GreenfootImage(153, 103);
    img.setColor(Color.CYAN);
    img.drawRect(50, 50, 101, 51);
    img.setColor(Color.BLACK);
    img.fillRect(51,51, 100, 50);
    getBackground().drawImage(img, 303, 50);
    addObject(new Label(" SOLO ",30), 402, 128);
    
    img = new GreenfootImage(303, 103);
    img.setColor(Color.CYAN);
    img.drawRect(50, 50, 187, 51);
    img.setColor(Color.BLACK);
    img.fillRect(51,51, 186, 50);
    getBackground().drawImage(img, 269, 250);
    addObject(new Label(" FEEDBACK ",30), 413, 328);
    
    img = new GreenfootImage(153, 106);
    img.setColor(new Color(Greenfoot.getRandomNumber(255)+1,Greenfoot.getRandomNumber(255),Greenfoot.getRandomNumber(255)));
    img.fillRect(48,48, 103, 56);
    img.setColor(Color.BLACK);
    img.fillRect(51,51, 97, 50);
    getBackground().drawImage(img, 302, 350);
    addObject(new Label(" HELP ",30), 400, 428);
    
    img = new GreenfootImage(303, 106);
    img.setColor(new Color(Greenfoot.getRandomNumber(255)+1,Greenfoot.getRandomNumber(255),Greenfoot.getRandomNumber(255)));
    img.fillRect(48, 48, 204, 57);
    img.setColor(Color.BLACK);
    img.fillRect(51,51, 197, 50);
    getBackground().drawImage(img, 272, 450);
    addObject(new Label(" HIGH SCORES ",30), 421, 528);
}
Roshan123 Roshan123

2020/12/22

#
Ohhh... Thanks a lot for rectifying my mistakes!!! _/\_
Roshan123 Roshan123

2020/12/22

#
And about line 79 to 51 I did this becaz 1) i wanted to get random colors for every fps
danpost danpost

2020/12/22

#
Roshan123 wrote...
And about line 79 to 51 I did this becaz 1) i wanted to get random colors for every fps
Okay, but don't change it every frame -- maybe every fourth or fifth frame. The human eye is limited to how many frames per second it can interpret. And only the color -- do not keep creating new Label objects.
Roshan123 Roshan123

2020/12/23

#
Yes Boss!!!
You need to login to post a reply.