title: Out of Memory Error: Java Heap space
body: <p>I'm trying to read a file using mapped byte buffer and am getting an <strong>OutOfMemoryError:JavaHeapSpace</strong> at the line <strong>buffer.position(position);</strong>.. I don't understand what's wrong in the code.. What could be the reason for the error?</p>
  
  <pre><code>private void readFile()
  {
      int lastPos = buffer.getInt();
  
      int dirLen = buffer.getInt();
      byte[] dirBytes = new byte[dirLen];
      buffer.get(dirBytes);
      this.dir = new String(dirBytes);
  
      int filePatternLen = buffer.getInt();
      byte[] filePatternBytes = new byte[filePatternLen];
      buffer.get(filePatternBytes);
      this.filePattern = new String(filePatternBytes);
  
      int numFileMetaDatas = 0;
      while (buffer.position() &lt; lastPos)
      {
          numFileMetaDatas++;
          int position = buffer.position();
          //Size is needed as we are reserving some extra bytes
          int size = buffer.getInt();
          buffer.position(position + ByteUtil.SIZE_OF_INT + ByteUtil.SIZE_OF_BYTE + ByteUtil.SIZE_OF_LONG + ByteUtil.SIZE_OF_LONG + ByteUtil.SIZE_OF_LONG);
          int fileNameLen = buffer.getInt();
          byte[] fileNameBytes = new byte[fileNameLen];
          buffer.get(fileNameBytes);
  
          FileMetaData fileMetaData = new FileMetaData(buffer, size, position);
          UniqueID uniqID = fileMetaData.getUniqueID();
  
          uniqIdVsFileMetaData.put(uniqID, fileMetaData);
  
          position = position + size;
          buffer.position(position);
      }
      nextMetaStartPos = lastPos;
  }
  </code></pre>