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.
Note: Screen resolution is 800x400 so I cant really resize image by factor 2 as it is suggested on android developer 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.
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.
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?
Here is code where I load images:
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));
Here is exact error from LogCat:
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
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.
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?
Background: Middle: Foreground
Note: This are just static backgrounds, animation happens around and between them.