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

2015/11/20

Getting Heap space error. Tried restarting. Didn't work. Continues to get errors.

Myopunk119 Myopunk119

2015/11/20

#
I created a boss level in my game. I intended to animate one of it's attacks by coding the animation in actor's code. I call it to spawn in The Boss's class but i get a heap space error when i run it on that level. At first it said that the error was when I called LaserAttack(). Now it's giving me this error. Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space at java.awt.image.DataBufferInt.<init>(DataBufferInt.java:75) at java.awt.image.Raster.createPackedRaster(Raster.java:467) at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1032) at java.awt.image.BufferedImage.<init>(BufferedImage.java:333) at com.sun.java.swing.plaf.windows.XPStyle$SkinPainter.createImage(XPStyle.java:681) at sun.swing.CachedPainter.paint0(CachedPainter.java:139) at sun.swing.CachedPainter.paint(CachedPainter.java:111) at com.sun.java.swing.plaf.windows.XPStyle$Skin.paintSkinRaw(XPStyle.java:615) at com.sun.java.swing.plaf.windows.XPStyle$Skin.paintSkin(XPStyle.java:594) at com.sun.java.swing.plaf.windows.XPStyle$XPImageBorder.paintBorder(XPStyle.java:370) at javax.swing.border.TitledBorder.paintBorder(TitledBorder.java:329) at javax.swing.JComponent.paintBorder(JComponent.java:949) at javax.swing.JComponent.paint(JComponent.java:1057) at javax.swing.JComponent.paintChildren(JComponent.java:889) at javax.swing.JComponent.paint(JComponent.java:1065) at javax.swing.JComponent.paintChildren(JComponent.java:889) at javax.swing.JComponent.paint(JComponent.java:1065) at javax.swing.JViewport.paint(JViewport.java:728) at javax.swing.JComponent.paintChildren(JComponent.java:889) at javax.swing.JComponent.paint(JComponent.java:1065) at javax.swing.JComponent.paintChildren(JComponent.java:889) at javax.swing.JComponent.paint(JComponent.java:1065) at javax.swing.JComponent.paintChildren(JComponent.java:889) at javax.swing.JComponent.paint(JComponent.java:1065) at javax.swing.JComponent.paintChildren(JComponent.java:889) at javax.swing.JComponent.paint(JComponent.java:1065) at javax.swing.JLayeredPane.paint(JLayeredPane.java:586) at javax.swing.JComponent.paintChildren(JComponent.java:889) at javax.swing.JComponent.paintToOffscreen(JComponent.java:5217) at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1579) at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1502) at javax.swing.RepaintManager.paint(RepaintManager.java:1272) Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space at java.security.ProtectionDomain$JavaSecurityAccessImpl.getCombinedACC(ProtectionDomain.java:91) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:78) at java.awt.EventQueue.dispatchEvent(EventQueue.java:726) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82) Exception in thread "AWT-Windows" java.lang.OutOfMemoryError: OutOfMemoryError at sun.awt.windows.WToolkit.eventLoop(Native Method) at sun.awt.windows.WToolkit.run(WToolkit.java:306) at java.lang.Thread.run(Thread.java:745) Exception in thread "SimulationThread" Exception in thread "RMI TCP Connection(idle)" java.lang.OutOfMemoryError: Java heap space Exception in thread "RMI TCP Connection(idle)" java.lang.OutOfMemoryError: Java heap space Exception in thread "RMI TCP Connection(idle)" java.lang.OutOfMemoryError: Java heap space Exception in thread "RMI TCP Connection(idle)" java.lang.OutOfMemoryError: Java heap space Exception in thread "RMI TCP Connection(idle)" java.lang.OutOfMemoryError: Java heap space Exception in thread "RMI TCP Connection(idle)" java.lang.OutOfMemoryError: Java heap space Exception in thread "RMI TCP Connection(idle)" java.lang.OutOfMemoryError: Java heap space Exception in thread "RMI TCP Connection(idle)" java.lang.OutOfMemoryError: Java heap space Exception in thread "RMI RenewClean-" java.lang.OutOfMemoryError: Java heap space Exception in thread "RMI TCP Connection(idle)" java.lang.OutOfMemoryError: Java heap space Here is my code for the boss.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Boss1 here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Boss1 extends Everyone
{
    private static GreenfootSound Beam = new GreenfootSound("BeamEffect.wav");
    private static int BHealth=500;
    private static int timeToBeam=200;
    /**
     * Act - do whatever the Boss1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        while(BHealth >= 250) {
            timeToBeam--;
            if(timeToBeam==0)
            {
                LaserAttack();
                timeToBeam= 200;
            }
             
    }   
    }
    public void LaserAttack()
    {
        getWorld().addObject(new BossBeam(), getX(), getY());
    }
         
}
here is the code for my laser Beam actor that is fired by my boss.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class BossBeam here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class BossBeam extends Actor
{
    private int atime=0;
    /**
     * Act - do whatever the BossBeam wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        while(atime <=20)
        {
            atime++;
            if(atime==3)
            {
                setImage("BeamAN2.png");
            }
            if(atime==4)
            {
                setImage("BeamAN3.png");
            }
            if(atime==5)
            {
                setImage("BeamAN4.png");
            }
            if(atime==6)
            {
                setImage("BeamAN5.png");
            }
            if(atime==7)
            {
                setImage("BeamAN6.png");
            }
            if(atime==8)
            {
                setImage("BeamAN7.png");
            }
            while(atime<14)
            {
                setImage("BeamAN8.png");
                setImage("BeamAN9.png");
            }
            if(atime==14)
            {
                setImage("BeamAN10.png");
            }
            if(atime==15)
            {
                setImage("BeamAN11.png");
            }
            if(atime==16)
            {
                setImage("BeamF12.png");
            }
            if(atime==17)
            {
                setImage("BeamF13.png");
            }
            if(atime==18)
            {
                setImage("BeamF14.png");
            }
            if(atime==19)
            {
                setImage("BeamF15.png");
            }
            if(atime==20)
            {
                getWorld().removeObject(this);
            }
        }
    }   
}
Royalblue64 Royalblue64

2015/11/20

#
The only obvious problem that I see in this code is that the BossBeam actor has a while loop in its act method. In this case, the loop is checking to see if atime is less than 14, which it obviously is when it spawns. This means that the program gets to this act method and is never able to exit the method. Typically, it's bad practice to use while loops within the act method anyway, so I definitely suggest getting rid of that.
You need to login to post a reply.