title: Android VM Won't let us allocate xx bytes
body: <p>I'm working on a android game. Problem starts when I try to use 3 images for background. Images are 1280x720px and 100kb large. 
  The images are really not that big, so I'm bit confused why they should cause memory problem. </p>
  
  <p>Note: Screen resolution is 800x400 so I cant really resize image by factor 2 as it is <a href="http://developer.android.com/training/displaying-bitmaps/load-bitmap.html" rel="nofollow noreferrer">suggested on android developer</a>
  Note: I'm using HTC desire phone (here crash comes), I've also tried it on Samsung Galaxy S1 and on Samsung it works OK.</p>
  
  <p>I've analysed memory in DDMS and what is strange is I found that Heap is only 3.5mb large. If I check rt.MaxMemory() it says I have around 25mb of space per app.</p>
  
  <p>What comes to my mind is that heap isn't "updating" fast enough to accommodate for larger photos/apps, so it crashes before it can load images despite being so small. Is there any way to manualy set heap size, or at least tell it to enlarge?</p>
  
  <p>Here is code where I load images:</p>
  
  <pre><code>        Bitmap floorFront = BitmapFactory.decodeResource(host.getResources(), R.drawable.floor1);
          floorFront = Bitmap.createScaledBitmap(floorFront,host.realWidth,host.realHeight, true);
          floorFront = Bitmap.createBitmap(floorFront, 0, host.realHeight/2, host.realWidth, host.realHeight/2);
          host.addInstance(new Background(0,host.realHeight/2,host,floorFront,1));
  
          Bitmap floorBack = BitmapFactory.decodeResource(host.getResources(), R.drawable.sand);
          floorBack = Bitmap.createScaledBitmap(floorBack,host.realWidth,host.realHeight, true);
          floorBack = Bitmap.createBitmap(floorBack, 0, host.realHeight/2, host.realWidth, host.realHeight/2);
          host.addInstance(new Background(0,host.realHeight/2,host,floorBack,4));
  
          Bitmap floorRock = BitmapFactory.decodeResource(host.getResources(), R.drawable.foreground);
          floorRock = Bitmap.createScaledBitmap(floorRock,host.realWidth,host.realHeight, true);
          floorRock = Bitmap.createBitmap(floorRock, 0, host.realHeight/2, host.realWidth, host.realHeight/2);
          host.addInstance(new Background(0,host.realHeight/2,host,floorRock,0));
  </code></pre>
  
  <p>Here is exact error from LogCat:</p>
  
  <pre><code> maxMemory:25165824
   memoryClass:24
   6144000-byte external allocation too large for tgis process.
   Out of memory: Heap Size=4739KB, Allocated=2399KB, Bitmap Size=18668KB
   VM won't let use allocate 6144000bytes
  </code></pre>
  
  <p>EDIT:
  Now that you said it makes sense that bitmaps use  “raw” size of image and not compressed. And after some research it seems that on android 2.2 and 2.3 bitmaps are not stored in heap therefore heap doesn’t increase after image is loaded. </p>
  
  <p>I have one more question. We use those 3 images for background. We will now resize them a bit in vertical direction, but in horizontal we still kind of need them as they are. So the image will still be around 100x720px.  The problem why we need 3 images is, so we can draw them separately and get layers effect (we can draw something between 2 images). What would be the best way to implement this to get desirable effect with as little of memory to use as possible?</p>
  
  <p>Background:
  <img src="https://i.stack.imgur.com/NwPsw.png" alt="background">
  Middle:
  <img src="https://i.stack.imgur.com/kwV6K.png" alt="middle">
  Foreground
  <img src="https://i.stack.imgur.com/gBWST.png" alt="foreground"></p>
  
  <p>Note: This are just static backgrounds, animation happens around and between them.</p>