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

2013/2/17

Java heap space ERROR

ajamesc97 ajamesc97

2013/2/17

#
Can someone try to help me identify what is causing these heap space errors?
java.lang.OutOfMemoryError: Java heap space
	at javax.imageio.stream.ImageInputStreamImpl.<init>(ImageInputStreamImpl.java:48)
	at javax.imageio.stream.FileCacheImageInputStream.<init>(FileCacheImageInputStream.java:76)
	at com.sun.imageio.spi.InputStreamImageInputStreamSpi.createInputStreamInstance(InputStreamImageInputStreamSpi.java:51)
	at javax.imageio.ImageIO.createImageInputStream(ImageIO.java:331)
	at javax.imageio.ImageIO.read(ImageIO.java:1371)
	at greenfoot.util.GraphicsUtilities.loadCompatibleTranslucentImage(GraphicsUtilities.java:224)
	at greenfoot.GreenfootImage.loadURL(GreenfootImage.java:268)
	at greenfoot.GreenfootImage.loadFile(GreenfootImage.java:295)
	at greenfoot.GreenfootImage.<init>(GreenfootImage.java:109)
	at Alien3.<init>(Alien3.java:18)
	at Alien3.create(Alien3.java:32)
	at Alien3.act(Alien3.java:25)
	at greenfoot.core.Simulation.actActor(Simulation.java:565)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:523)
	at greenfoot.core.Simulation.runContent(Simulation.java:213)
	at greenfoot.core.Simulation.run(Simulation.java:203)
2013-02-17 14:24:21.111 java[4219:707] java.lang.OutOfMemoryError: Java heap space
	at com.apple.eawt._AppEventHandler.handleNativeNotification(_AppEventHandler.java:198)
2013-02-17 14:24:21.200 java[4219:707] (
	0   CoreFoundation                      0x00007fff8e9b00a6 __exceptionPreprocess + 198
	1   libobjc.A.dylib                     0x00007fff86afb3f0 objc_exception_throw + 43
	2   CoreFoundation                      0x00007fff8ea44229 -[NSException raise] + 9
	3   JavaNativeFoundation                0x000000010ae2cc3f JNFCallStaticVoidMethod + 213
	4   libawt.jnilib                       0x0000000112d71f4c +[ApplicationDelegate _notifyJava:] + 51
	5   CoreFoundation                      0x00007fff8e96247a _CFXNotificationPost + 2554
	6   Foundation                          0x00007fff8972c846 -[NSNotificationCenter postNotificationName:object:userInfo:] + 64
	7   AppKit                              0x00007fff8715b9c2 -[NSApplication sendEvent:] + 2303
	8   AppKit                              0x00007fff8768710f _NXFinishActivation + 153
	9   AppKit                              0x00007fff87160018 -[NSWindow sendEvent:] + 5327
	10  libawt.jnilib                       0x0000000112d9599c -[CocoaAppWindow sendEvent:] + 185
	11  AppKit                              0x00007fff8715c744 -[NSApplication sendEvent:] + 5761
	12  AppKit                              0x00007fff870722fa -[NSApplication run] + 636
	13  libawt.jnilib                       0x0000000112d70680 +[AWTStarter startAWT:] + 1495
	14  libawt.jnilib                       0x0000000112d6fffa -[CPerformer perform] + 93
	15  Foundation                          0x00007fff89776677 __NSThreadPerformPerform + 225
	16  CoreFoundation                      0x00007fff8e92f101 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
	17  CoreFoundation                      0x00007fff8e92ea25 __CFRunLoopDoSources0 + 245
	18  CoreFoundation                      0x00007fff8e951dc5 __CFRunLoopRun + 789
	19  CoreFoundation                      0x00007fff8e9516b2 CFRunLoopRunSpecific + 290
	20  java                                0x000000010a549843 java + 18499
	21  java                                0x000000010a54929a java + 17050
	22  java                                0x000000010a546a98 java + 6808
)
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
	at java.awt.image.DataBufferInt.<init>(DataBufferInt.java:41)
	at java.awt.image.Raster.createPackedRaster(Raster.java:458)
	at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1015)
	at java.awt.image.BufferedImage.<init>(BufferedImage.java:324)
	at apple.awt.CGraphicsConfig.createCompatibleImage(CGraphicsConfig.java:133)
	at greenfoot.util.GraphicsUtilities.toCompatibleTranslucentImage(GraphicsUtilities.java:297)
	at greenfoot.util.GraphicsUtilities.loadCompatibleTranslucentImage(GraphicsUtilities.java:228)
	at greenfoot.GreenfootImage.loadURL(GreenfootImage.java:268)
	at greenfoot.GreenfootImage.loadFile(GreenfootImage.java:295)
	at greenfoot.GreenfootImage.<init>(GreenfootImage.java:109)
	at greenfoot.util.GreenfootUtil.getGreenfootImage(GreenfootUtil.java:797)
	at greenfoot.core.ProjectProperties.getImage(ProjectProperties.java:269)
	at greenfoot.gui.classbrowser.role.ImageClassRole.getClassThatHasImage(ImageClassRole.java:98)
	at greenfoot.gui.classbrowser.role.ImageClassRole.getGreenfootImage(ImageClassRole.java:109)
	at greenfoot.gui.classbrowser.role.ImageClassRole.getImage(ImageClassRole.java:80)
	at greenfoot.gui.classbrowser.role.ImageClassRole.changeImage(ImageClassRole.java:156)
	at greenfoot.gui.classbrowser.role.ImageClassRole.buildUI(ImageClassRole.java:63)
	at greenfoot.gui.classbrowser.ClassView.update(ClassView.java:188)
	at greenfoot.gui.classbrowser.ClassView.updateView(ClassView.java:412)
	at greenfoot.core.GClass$2.run(GClass.java:600)
	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:702)
	at java.awt.EventQueue.access$400(EventQueue.java:82)
	at java.awt.EventQueue$2.run(EventQueue.java:663)
	at java.awt.EventQueue$2.run(EventQueue.java:661)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:672)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
java.lang.OutOfMemoryError: Java heap space
java.lang.OutOfMemoryError: Java heap space
java.lang.OutOfMemoryError: Java heap space
java.lang.OutOfMemoryError: Java heap space
java.lang.OutOfMemoryError: Java heap space
java.lang.OutOfMemoryError: Java heap space
java.lang.OutOfMemoryError: Java heap space
It was doing everything it was supposed to when I was testing it, then I compiled it again and it gave me this. What can I do to fix it?
danpost danpost

2013/2/17

#
It looks like it was trying to load image files at the time. Maybe you have an excessive amount of images or very large images in your scenario. Sometimes closing the application and opening it back up will temporarily fix the problem until you compile several times.
ajamesc97 ajamesc97

2013/2/17

#
After restarting it once it did nothing, but the second time i stopped getting the heap space error temporarily before i got it again. Also i only have 6 pictures, the biggest of them being 15kb, so i wouldn't think that's the problem. It might have something to do with another error i got a while before the heap space error. I have a spaceship that shoots bullets like in space invaders, and when the bullet gets to the edge of the world i get this error.
java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed.
	at greenfoot.Actor.failIfNotInWorld(Actor.java:663)
	at greenfoot.Actor.getOneObjectAtOffset(Actor.java:867)
	at Bullet.canSee(Bullet.java:30)
	at Bullet.killAlien(Bullet.java:43)
	at Bullet.act(Bullet.java:18)
	at greenfoot.core.Simulation.actActor(Simulation.java:565)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:523)
	at greenfoot.core.Simulation.runContent(Simulation.java:213)
	at greenfoot.core.Simulation.run(Simulation.java:203)
Here is the code for the bullet.
public class Bullet extends Actor
{
    public void act() 
    {
        move();
        killAlien();
    }    
    public void move()
    {
        setLocation(getX(),getY()-2);
        if(getY()<2)
        {
            getWorld().removeObject(this);
        }
    }
    public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;        
    }
    public void eat(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        if(actor != null) 
        {
            getWorld().removeObject(actor);
        }
    }
    public void killAlien()
    {
        if(canSee(Alien.class))
        {
            eat(Alien.class);
            getWorld().removeObject(this);
        }
    }
}
danpost danpost

2013/2/17

#
The two errors are not related in any way. Both methods called from your 'act' method have the possibility of removing the bullet from the world and both also require that the location of the object be in the world. If the first method removes the object from the world, the second one will err because the bullet is not in the world and its location in a null world is just not possible to acquire. You can add a check to see if the bullet in still in the world after the first method call.
// either
public void act()
{
    move();
    if (getWorld() == null) return;
    killAlien();
}
// or instead
public void act()
{
    move();
    if (getWorld() != null) killAlien();
}
ajamesc97 ajamesc97

2013/2/17

#
Thanks. How would i change the bullet so that it doesn't shoot if there is already a bullet in the world? As of now i'm just using a timer so that the bullet number is limited to one every fifty acts, but i want it to be more authentic to the original game.
    int t=50;
    public void act() 
    {
        checkKeypress();
        t=t+1;
    }    
    public void checkKeypress()
    {
        if(Greenfoot.isKeyDown("space"))
        {
            shoot();
        }
    }
    public void shoot()
    {
        if(t>50)
        {
            getWorld().addObject(new Bullet(),getX(),getY()-2);
            t=0;
        }
    }
danpost danpost

2013/2/18

#
Just remove the timer and change the condition.
public void act() 
{
    checkKeypress();
}    

public void checkKeypress()
{
    if(Greenfoot.isKeyDown("space"))
    {
        shoot();
    }
}

public void shoot()
{
    if(getWorld().getObjects(Bullet.class).isEmpty())
    {
        getWorld().addObject(new Bullet(),getX(),getY()-2);
    }
}
ajamesc97 ajamesc97

2013/2/18

#
Thanks! You've been a great help.
You need to login to post a reply.