title: Optimal data structures for a tile-based RPG In java
body: <p>The game is tile-based, but the tiles are really only for terrain and path-finding purposes. Sprite movement is free-form (ie, the player can be half way through a tile).</p>
  
  <p>The maps in this game are very large. At normal zoom tiles are 32*32 pixels, and maps sizes can be up 2000x2000 or larger (4 million tiles!). Currently, a map is an array of tiles, and the tile object looks like this:</p>
  
  <pre><code>public class Tile {
  
      public byte groundType;
      public byte featureType;
      public ArrayList&lt;Sprite&gt; entities;
  
      public Tile () {
          groundType = -1;
          featureType = -1;
          entities = null;
      }
  }    
  </code></pre>
  
  <p>Where groundType is the texture, and featureType is a map object that takes up an entire tile (such as a tree, or large rock). These types of features are quite common so I have opted to make them their own variable rather than store them in entities, which is a list of objects on the tile (items, creatures, etc). Entities are saved to a tile for performance reasons.</p>
  
  <p>The problem I am having is that if entities is not initialized to null, Java runs out of heap space. But setting it to null and only initializing when something moves into the tile seems to me a bad solution. If a creature were moving across otherwise empty tiles, lists would constantly need to be initialized and set back to null. Is this not poor memory management? What would be a better solution?</p>