Okay so im programming a game for my A-Level computing project, and so far everything has gone okay. I finished the framework, and started implementing gameplay when i got a java.lang.OutOfMemoryError: Java heap space
(full error below). I've never experienced this before, however could it be to do with me trying to load too many static variables? For example, each of my levels are static, and each one is trying to render an image that is 600x600 pixels into a map. As it is static would this cause the memory to crash dump?
Here is some code at the lines the output has stated the error is at:
SpriteSheet.java, line 131:
129 private void load() {
130 try {
131 image = ImageIO.read(SpriteSheet.class.getResource(path));
132 this.width = image.getWidth();
133 this.height = image.getHeight();
134 pixels = new int[this.width * this.height];
135 image.getRGB(0, 0, this.width, this.height, pixels, 0, this.width);
136 } catch (IOException e) {} catch (Exception e) {}
137 }
SpriteSheet.java line 110 calls the above method from the constructor.
Enemy, line 161:
160 public void initSheets() {
161 SpriteSheet mainSheet = new SpriteSheet(sheet, 96, 128);
162 down = new AnimatedObject(new SpriteSheet(mainSheet, 0, 0, 3, 1, 32), 32, 32, 3);
163 up = new AnimatedObject(new SpriteSheet(mainSheet, 0, 3, 3, 1, 32), 32, 32, 3);
164 left = new AnimatedObject(new SpriteSheet(mainSheet, 0, 1, 3, 1, 32), 32, 32, 3);
165 right = new AnimatedObject(new SpriteSheet(mainSheet, 0, 2, 3, 1, 32), 32, 32, 3);
166 }
Zombie.java, line 25 calls the above method from the constructor.
EnemySpawner.java, line 35 creates a new Zombie, in which initSheets()
is called.
BossLevel.java, line 42:
38 protected void generateLevel(){
39 for (int x = 0; x < width; x++){
40 for (int y = 0; y < height; y++){
41 if (tiles[x + y * width] == Tile.col_enemy) {
42 EnemySpawner es = new EnemySpawner(x, y, 100, 5, this, bossName);
43 add(es);
44 } else if (tiles[x + y * width] == Tile.col_boss){
45 Boss bs = new Boss(x, y,health, "/textures/sheets/mob/enemy/" + bossName.toLowerCase() + "/king" + bossName + ".png");
46 add(bs);
47 }
48 }
49 }
50 }
Finally, in Level.java, this is the code generating my levels:
53 public static Level level1 = new BossLevel("/levels/level1.png", "Zombie", 60);
54 public static Level level2 = new LavaLevel("/levels/level2.png", "Mummy", 120);
55 public static Level level3 = new BossLevel("/levels/level1.png", "Goblin", 180);
56 public static Level level4 = new BossLevel("/levels/level1.png", "Mummy", 240);
57 public static Level level5 = new BossLevel("/levels/level1.png", "Goblin", 180);
58 public static Level level6 = new BossLevel("/levels/level1.png", "Zombie", 60);
59 public static Level spawn = new SpawnLevel("/levels/spawn.png");
...
88 public static void initLevels(){
89 Level.level1 = new BossLevel("/levels/level1.png", "Zombie", 60);
90 Level.level2 = new LavaLevel("/levels/level1.png", "Mummy", 120);
91 Level.level3 = new BossLevel("/levels/level1.png", "Goblin", 180);
92 Level.level4 = new BossLevel("/levels/level1.png", "Mummy", 240);
93 Level.level5 = new BossLevel("/levels/level1.png", "Goblin", 180);
94 Level.level6 = new BossLevel("/levels/level1.png", "Zombie", 60);
95 Level.spawn = new SpawnLevel("/levels/spawn.png");
96 generatedPortals = false;
97 }
Last thing, it only occurs if I have 6 levels. If I remove the code for the 6th level it works fine, but it does take ages for the game to load. Any ideas?
Thanks
System specs: Celeron 2.60GHz runnig at 3.7 GHz 4GB RAM Windows 10 x64 bit