title: Android: OutofMemoryError: bitmap size exceeds VM budget with no reason I can see
body: <p>I am having an OutOfMemory exception with a gallery over 600x800 pixels JPEG's.</p>
  
  <hr>
  
  <p><strong>The environment</strong></p>
  
  <p>I've been using Gallery with JPG images around 600x800 pixels.</p>
  
  <p>Since my content may be a bit more complex than just images, I have set each view to be a RelativeLayout that wraps ImageView with the JPG.</p>
  
  <p>In order to "speed up" the user experience I have a simple cache of 4 slots that prefetches (in a looper) about 1 image left and 1 image right to the displayed image and keeps them in a 4 slot HashMap. </p>
  
  <p><strong>The platform</strong></p>
  
  <p>I am using AVD of 256 RAM and 128 Heap Size, with a 600x800 screen.
  It also happens on an Entourage Edge target, except that with the device it's harder to debug.</p>
  
  <hr>
  
  <p><strong>The problem</strong></p>
  
  <p>I have been getting an exception:</p>
  
  <pre><code>OutofMemoryError: bitmap size exceeds VM budget
  </code></pre>
  
  <p>And it happens when fetching the fifth image. I have tried to change the size of my image cache, and it is still the same.</p>
  
  <hr>
  
  <p><strong>The strange thing: There should not be a memory problem</strong></p>
  
  <p>In order to make sure the heap limit is very far away from what I need, I have defined a dummy 8MB array in the beginning, and left it unreferenced so it's immediately dispatched. It is a member of the activity thread and is defined as following</p>
  
  <pre><code>static { @SuppressWarnings("unused")
  byte dummy[] = new byte[ 8*1024*1024 ]; }    
  </code></pre>
  
  <p>The result is that the heap size is nearly 11MB and it's all free.
  <em>Note</em> I have added that trick after it began to crash. It makes OutOfMemory less frequent.</p>
  
  <p>Now, I am using DDMS. Just before the crash (does not change much after the crash), DDMS shows:</p>
  
  <pre><code>ID  Heap Size   Allocated   Free       %Used    #Objects
  1   11.195 MB   2.428 MB    8.767 MB   21.69%   47,156  
  </code></pre>
  
  <p>And in the detail table it shows:</p>
  
  <pre><code>Type  Count  Total Size   Smallest   Largest   Median    Average
  free  1,536  8.739MB      16B        7.750MB   24B       5.825KB
  </code></pre>
  
  <p>The largest block is 7.7MB. And yet the LogCat says:</p>
  
  <pre><code>ERROR/dalvikvm-heap(1923): 925200-byte external allocation too large for this process.
  </code></pre>
  
  <p>If you mind the relation of the median and the average, it is plausible to assume that most of the available blocks are very small. However, <strong>there is a block large enough for the bitmap, it's 7.7M. How come it is still not enough?</strong></p>
  
  <p>Note: I recorded a heap trace. When looking at the amount of data allocated, it does not feel like more than 2M is allocated. It does match the free memory report by DDMS.</p>
  
  <hr>
  
  <ul>
  <li>Could it be that I experience some problem like heap-fragmentation?</li>
  <li>How do I solve/workaround the problem?</li>
  <li>Is the heap shared to all threads?</li>
  <li>Could it be that I interpret the DDMS readout in a wrong way, and there is really no 900K block to allocate? If so, can anybody please tell me where I can see that?</li>
  </ul>
  
  <p>Thanks a lot</p>
  
  <p>Meymann</p>