title: Android - BitmapFactory.decodeByteArray - OutOfMemoryError (OOM)
body: <p>I have read 100s of article about the OOM problem. Most are in regard to large bitmaps. I am doing a mapping application where we download <code>256x256</code> weather overlay tiles. Most are totally transparent and very small. I just got a crash on a bitmap stream that was 442 Bytes long while calling <code>BitmapFactory.decodeByteArray(....).</code> </p>
  
  <p>The Exception states:</p>
  
  <pre><code>java.lang.OutOfMemoryError: bitmap size exceeds VM budget(Heap Size=9415KB, Allocated=5192KB, Bitmap Size=23671KB)
  </code></pre>
  
  <p>The code is:</p>
  
  <pre><code>protected Bitmap retrieveImageData() throws IOException {
      URL url = new URL(imageUrl);
      InputStream in = null;
      OutputStream out = null;
      HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  
      // determine the image size and allocate a buffer
      int fileSize = connection.getContentLength();
      if (fileSize &lt; 0) {
          return null;
      }
      byte[] imageData = new byte[fileSize];
  
      // download the file
      //Log.d(LOG_TAG, "fetching image " + imageUrl + " (" + fileSize + ")");
      BufferedInputStream istream = new BufferedInputStream(connection.getInputStream());
      int bytesRead = 0;
      int offset = 0;
      while (bytesRead != -1 &amp;&amp; offset &lt; fileSize) {
          bytesRead = istream.read(imageData, offset, fileSize - offset);
          offset += bytesRead;
      }
  
      // clean up
      istream.close();
      connection.disconnect();
      Bitmap bitmap = null;
      try {
          bitmap = BitmapFactory.decodeByteArray(imageData, 0, bytesRead);
      } catch (OutOfMemoryError e) {
          Log.e("Map", "Tile Loader (241) Out Of Memory Error " + e.getLocalizedMessage());
          System.gc();
      }
      return bitmap;
  
  }
  </code></pre>
  
  <p>Here is what I see in the debugger:</p>
  
  <pre><code>bytesRead = 442
  </code></pre>
  
  <p>So the Bitmap data is 442 Bytes. Why would it be trying to create a 23671KB Bitmap and running out of memory?</p>