samedi 29 novembre 2014

OutOfMemoryError Java Heap Space due to initializing instance variables in constructer?


For a game I am making I have an Explosion class. Here is the first part:



public class Explosion extends Rectangle {
private static final int FRAMES = 40;
public static Texture expStrip;
private static TextureRegion[] expFrames;
private static Animation expAnimation;

public TextureRegion currentFrame;
public float stateTime;

public Explosion() {
super();
stateTime = 0;
//width = 32;
//height = 32;
}


The code runs as is and I can instantiate as many explosions as I need, but when I uncomment the initializations for width and height in the constructer (the width and height variables are inherited from the superclass Rectangle), the first time an explosion is created i get an OutOfMemoryError - Java Heap Space. I understand you can increase the heap size, but I am new to java and wish to understand what heap space is. From this article http://javaeesupportpatterns.blogspot.com/2011/08/java-heap-space-hotspot-vm.html I understand that heap space is used to store my primary class instances. So why does setting the width and height causes this error? Wouldn't the error more likely be caused by creating too many Explosion instances?





Aucun commentaire:

Enregistrer un commentaire