title: LWJGL 2D Tile game using more than 50MB of RAM a secon
body: <p>So I am a relatively new to programming and I am trying to make 2D game using LWJGL.  I have made a background map using tiles in an array list, I then update the necessary things such as the player etc, then I draw them onto the screen, and update the screen. I have seen other people do this just fine but whenever I do it uses up almost 100 mb of RAM every second, causing my computer to freeze if the game runs for 30 seconds or less.  I am not sure if it's the drawing that is causing this memory usage or something else, I have checked to make sure no code is being ran without my knowledge of it, please if someone should tell me what is wrong that would be great.  I have provided the main classes used in my game, there about 10 total but I have only shown 3 as I think it is enough to get my point. I know some of the code could look much better but, like I said, I am new to all of this. Thank you!</p>
  
  <p>BOOT CLASS</p>
  
  <p>public class Boot {</p>
  
  <pre><code>public Boot() {
      beginSession();
  
      tile menu = new tile(0, 0, 1024, 1024, TileType.Menu);
      tile outline = new tile(0, 0, 395, 96, TileType.Outline);
      Display.sync(60);
      while (!Display.isCloseRequested()) {
          System.out.println("MENU");
          menu.Draw();
          if (Display.isCloseRequested()) {
              Display.destroy();
          }
          if (Mouse.getX() &gt; 380 &amp;&amp; Mouse.getX() &lt; 650 &amp;&amp; Mouse.getY() &lt; 320 &amp;&amp; Mouse.getY() &gt; 250) {
              outline.setX(322);
              outline.setY(305);
              outline.Draw();
          }
          if (Mouse.isButtonDown(0) &amp;&amp; Mouse.getX() &gt; 380 &amp;&amp; Mouse.getX() &lt; 650 &amp;&amp; Mouse.getY() &lt; 320 &amp;&amp; Mouse.getY() &gt; 250) {
              new Play();
  
          }
          if (Mouse.getX() &gt; 340 &amp;&amp; Mouse.getX() &lt; 700 &amp;&amp; Mouse.getY() &lt; 210 &amp;&amp; Mouse.getY() &gt; 130) {
              outline.setX(322);
              outline.setY(425);
              outline.Draw();
          }
  
          if (Mouse.isButtonDown(0) &amp;&amp; Mouse.getX() &gt; 340 &amp;&amp; Mouse.getX() &lt; 700 &amp;&amp; Mouse.getY() &lt; 210 &amp;&amp; Mouse.getY() &gt; 130) {
              new About();
          }
          Display.update();
      }
  }
  
  public static void main(String[] args) {
      new Boot();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>PLAY CLASS`</p>
  
  <p>public class Play {</p>
  
  <pre><code>public Play() {
      //@formatter:off
      //MAPS//
      int[][] map0 = { 
              { 6, 6, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
              { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
              { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
              { 6, 0, 6, 6, 6, 6, 6, 0, 6, 6, 6, 0, 6, 6, 6, 6 },
              { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
              { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
              { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
              { 6, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6, 0, 6, 6, 6, 6 },
              { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 0 },
              { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 }, };
  
      int[][] map1 = {
              { 6, 6, 0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 0, 6, 6, 6 },
              { 0, 6, 6, 6, 6, 6, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6 },
              { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 0, 6, 6, 6 },
              { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
              { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
              { 6, 6, 6, 6, 6, 6, 6, 6, 6, 0, 6, 6, 6, 6, 6, 6 },
              { 6, 6, 6, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6, 0, 6, 6 },
              { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
              { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
              { 6, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6, 0, 6, 6, 6, 6 }, };
  
      int[][] map2 = { 
              { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
              { 6, 6, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
              { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 0, 6, 6, 6, 6 },
              { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
              { 0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 0, 6, 6 },
              { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
              { 6, 6, 6, 6, 6, 6, 0, 6, 6, 6, 6, 0, 6, 6, 6, 6 },
              { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
              { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
              { 6, 0, 6, 6, 6, 6, 6, 6, 0, 6, 6, 6, 6, 6, 6, 0 }, };
  
      int[][] map3 = {
              { 6, 6, 6, 6, 6, 6, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6 },
              { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
              { 6, 6, 6, 6, 6, 0, 6, 7, 6, 6, 6, 6, 6, 6, 6, 6 },
              { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
              { 6, 6, 6, 6, 6, 6, 6, 6, 6, 0, 6, 6, 6, 6, 6, 6 },
              { 6, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
              { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
              { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 },
              { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 },
              { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 }, };
      //MAP FOUR//
      int[][] map3left1 = {
              { 6, 6, 6, 6, 6, 6, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6 },
              { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
              { 6, 6, 6, 6, 6, 0, 6, 7, 6, 6, 6, 6, 6, 6, 6, 6 },
              { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
              { 6, 6, 6, 6, 6, 6, 6, 6, 6, 0, 6, 6, 6, 6, 6, 6 },
              { 6, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
              { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
              { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 },
              { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 },
              { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 }, };
      //MAP FIVE//
      int[][] map3left2 = {
              { 6, 6, 6, 6, 6, 6, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6 },
              { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
              { 6, 6, 6, 6, 6, 0, 6, 7, 6, 6, 6, 6, 6, 6, 6, 6 },
              { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
              { 6, 6, 6, 6, 6, 6, 6, 6, 6, 0, 6, 6, 6, 6, 6, 6 },
              { 6, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
              { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
              { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 },
              { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 },
              { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 }, };
      //MAP SIX//
      int[][] map3left3 = {
              { 6, 6, 6, 6, 6, 6, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6 },
              { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
              { 6, 6, 6, 6, 6, 0, 6, 7, 6, 6, 6, 6, 6, 6, 6, 6 },
              { 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
              { 6, 6, 6, 6, 6, 6, 6, 6, 6, 0, 6, 6, 6, 6, 6, 6 },
              { 6, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6 },
              { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
              { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 },
              { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 },
              { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 }, };
      //@formatter:on
  
      TileGrid grid0 = new TileGrid(map0);
      TileGrid grid1 = new TileGrid(map1);
      TileGrid grid2 = new TileGrid(map2);
      TileGrid grid3 = new TileGrid(map3);
      TileGrid grid3left1 = new TileGrid(map3left1);
      TileGrid grid3left2 = new TileGrid(map3left2);
      TileGrid grid3left3 = new TileGrid(map3left3);
  
      // ALL TILES IN GAME THAT ARE ALWAYS NEEDED GO HERE//
      Asteriod a = new Asteriod(quickLoad("asteriodfour"), -100, 0, grid0, 64, 64, 10, 100);
      Lander l = new Lander(quickLoad("LunarLander"), grid0.getTile(7, 0), grid0, 240, 228, 0, 0);
      Wave wave = new Wave(15, a);
      Astronaut player = new Astronaut(quickLoad("astronaut"), l.getX(), 310, 256, 256);
  
      // LADNING THE LANDER//
      while (l.isLanded() == false) {
          Display.sync(60);
          gridType = 3;
          if (Display.isCloseRequested()) {
              Display.destroy();
          }
  
          switch (l.getMapType()) {
          case 0:
              grid0.Draw();
              break;
          case 1:
              grid1.Draw();
              break;
          case 2:
              grid2.Draw();
              break;
          case 3:
              grid3.Draw();
              break;
          }
  
          l.Draw();
          l.UpdateLander();
  
          if (l.isLanded() == false &amp;&amp; l.isFirst()) {
              wave.UpdateAsteroid();
          } else {
              wave.getAsteriodList().clear();
          }
  
          if (wave.getColY() &gt; 390 &amp;&amp; l.getMapType() == 3) {
              wave.getAsteriodList().clear();
          }
  
          if (l.getX() - wave.getColX() &lt; 35 &amp;&amp; wave.getColX() - l.getX() &lt; 95 &amp;&amp; l.getY() - wave.getColY() &lt; 35 &amp;&amp; wave.getColY() - l.getY() &lt; 200) {
              wave.explodeAsteroid();
              l.takeDamage();
          }
  
          Display.update();
      }
      onMoon = true;
  
      // PLAYER EVERYTHING//
      while (onMoon == true) {
          Display.sync(60);
          if (Display.isCloseRequested()) {
              Display.destroy();
          }
  
  
          if (player.getX() &lt; 5 &amp;&amp; gridType == 3) {
              gridType = 4;
              player.setX(1000);
          } 
  
          switch (gridType) {
          case 3: 
              grid3.Draw();
              l.Draw();
              l.UpdateLander();
              break;
  
          case 4 :
              grid3left1.Draw();
          }
  
          player.Draw();
          player.UpdateAstronaut();
  
          if (player.isLeft() == true) {
              player.setTexture(quickLoad("astronaut"));
          } else {
              player.setTexture(quickLoad("astronautRight"));
          }
  
          Display.update();
      }
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>LANDER CLASS </p>
  
  <p>public class Lander extends Artist {</p>
  
  <pre><code>public Lander(Texture texture, tile startTile, TileGrid grid, int width, int height, float speed, int angle) {
      this.texture = texture;
      this.x = startTile.getX();
      this.y = startTile.getY();
      this.width = width;
      this.height = height;
      this.angle = angle;
  }
  
  public void Draw() {
      drawQuadTexRotate(texture, x, y, width, height, angle);
  }
  
  //UPDATES LANDER//
  public void UpdateLander() {
  
      tile forceField = new tile(685, 510, 256, 32, TileType.ForceField);
      tile forceFieldOff = new tile(745, offY, 64, 32, TileType.Off);
      tile forceFieldOn = new tile(755, onY, 32, 32, TileType.On);
      tile battery = new tile(13 * 64, 8 * 64, 96, 96, TileType.Battery8);
      tile outOfFuel = new tile(820, 480, outOfFuelX, 32, TileType.OutOfFuel);
  
      forceField.Draw();
      forceFieldOn.Draw();
      forceFieldOff.Draw();
      battery.setTexture(quickLoad(percentage()));
      outOfFuel.Draw();
      battery.Draw();
  
      screenMove();
      landerHealth();
  
      if (landed == false)
          y = (float) (y + 1.4);
  
      if (canMove == true) {
          if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
              x -= 1;
              if (angle &gt; -35) {
                  angle -= 0.2;
              }
              fuelUsed += 0.1;
              if (fuelUsed &gt; 70) {
                  fuelLeft -= 1;
                  fuelUsed = 0;
              }
          }
  
          if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
              y -= 0.6;
              fuelUsed += 0.1;
              if (fuelUsed &gt; 70) {
                  fuelLeft -= 1;
                  fuelUsed = 0;
              }
  
          }
          if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
              if (angle &lt; 25) {
                  angle += 0.2;
              }
              x += 1;
              fuelUsed += 0.1;
              if (fuelUsed &gt; 70) {
                  fuelLeft -= 1;
                  fuelUsed = 0;
              }
  
          }
          if (fuelLeft &lt; 0.1) {
              outOfFuelX = 170;
              canMove = false;
          }
      }
  }
  
  
  //LANDER HEALTH ONLY RAN WHEN UPDATED//
  public void landerHealth() {
  
      tile h = new tile(680, 540, 180, 96, TileType.Health100);
  
      if (health &lt; 90 &amp;&amp; health &gt; 80) {
          h.setTexture(quickLoad("health90"));
          h.Draw();
      }
      if (health &lt; 80 &amp;&amp; health &gt; 70) {
          h.setTexture(quickLoad("health80"));
          h.Draw();
      }
      if (health &lt; 70 &amp;&amp; health &gt; 60) {
          h.setTexture(quickLoad("health70"));
          h.Draw();
      }
      if (health &lt; 60 &amp;&amp; health &gt; 50) {
          h.setTexture(quickLoad("health60"));
          h.Draw();
      }
      if (health &lt; 50 &amp;&amp; health &gt; 40) {
          h.setTexture(quickLoad("health50"));
          h.Draw();
      }
      if (health &lt; 40 &amp;&amp; health &gt; 30) {
          h.setTexture(quickLoad("health40"));
          h.Draw();
      }
      if (health &lt; 30 &amp;&amp; health &gt; 20) {
          h.setTexture(quickLoad("health30"));
          h.Draw();
      }
      if (health &lt; 20 &amp;&amp; health &gt; 10) {
          h.setTexture(quickLoad("health20"));
          h.Draw();
      }
      if (health &lt; 10 &amp;&amp; health &gt; 0) {
          h.setTexture(quickLoad("health10"));
          h.Draw();
      }
      if (health &lt;= 0) {
          h.setTexture(quickLoad("health0"));
          h.Draw();
          landerExplode();
          canMove = false;
      }
      h.Draw();
  }
  
  
  //SCREEN MOVE ONLY RAN WHEN UPDATED//
  public void screenMove() {
      System.out.println("SCREEN MOVE");
      if (landed == false) {
          if (y &gt; 500 &amp;&amp; mapType &lt; 3) {
              mapType++;
              y -= 650;
          }
          if (mapType == 3) {
              if (y &lt; 230 &amp;&amp; y &gt; 226 &amp;&amp; angle &lt; 4 &amp;&amp; angle &gt; -4) {
  
                  canMove = false;
                  landed = true;
                  eX2 = x - 15;
                  eY2 = y - 15;
                  offY = 2000;
                  onY = 540;
  
              }
              if (y &gt; 234 &amp;&amp; (angle &gt; 4 || angle &lt; -4)) {
                  landerExplode();
              }
          }
      }
  }
  
  
  //ONLY RAN WHEN UPDATED, DEATH SCREEN NEEDED//
  public void landerExplode() {
      if (landerExplodeCounter &lt; 850) {
          if (first) {
              eX1 = x;
              eX2 = x - 15;
              eX3 = x;
              eY1 = y;
              eY2 = y - 15;
              eY3 = y;
              x = 2000;
              first = false;
          }
          System.out.println("EX");
          landerExplodeCounter++;
          tile landerp1 = new tile(x, y, 240, 228, TileType.LanderPart1);
          tile landerp2 = new tile(x, y, 240, 228, TileType.LanderPart2);
          tile landerp3 = new tile(x, y, 240, 228, TileType.LanderPart3);
          landerp1.setX(eX1 -= 0.7);
          landerp1.setY(eY1 -= 0.4);
          landerp2.setX(eX2 += 0.5);
          landerp2.setY(eY2 -= 0.2);
          landerp3.setX(eX3 -= 0.1);
          landerp3.setY(eY3 += 0.8);
          landerp1.Draw();
          landerp2.Draw();
          landerp3.Draw();
  
      }
  }
  
  //ONLY RAN WHEN UPDATED//
  public void takeDamage() {
      System.out.println("TAKE DAMAGE");
      if (landed == false) {
          health = (float) (health - 5.5);
          System.out.println(health);
      }
  }
  
  //ONLY RAN WHEN UPDATED//
  private String percentage() {
      System.out.println("PERCENTAGE");
      String name = "Battery";
      switch (fuelLeft) {
      case 8:
          name = name + "8";
          break;
      case 7:
          name = name + "7";
          break;
      case 6:
          name = name + "6";
          break;
      case 5:
          name = name + "5";
          break;
      case 4:
          name = name + "4";
          break;
      case 3:
          name = name + "3";
          break;
      case 2:
          name = name + "2";
          break;
      case 1:
          name = name + "1";
          break;
      case 0:
          name = name + "0";
          break;
      }
      return name;
  }
  </code></pre>