0

In my game, I have a bitmap image which functions as the games background (drawn on a canvas). The bigger the image the more "space" there is in the game for the user to move in, so I want the bitmap to be as big as possible without causing memory problems.

Well I went overboard and drew an enormous bitmap, which caused an out of memory error immediately on initialzation, so I shrunk the bitmap a bit. But I think its too small. My question is how can I measure the "safe" limit of the memory being used?

I used the following methods and read the output in CatLog:

Debug.getNativeHeapAllocatedSize();
Log.v("MEMORY",String.valueOf(Debug.getNativeHeapAllocatedSize()) );                    
Log.v("FreeMEMORY",String.valueOf(Debug.getNativeHeapFreeSize ()) );

The results of which were:

 05-25 09:53:54.044: VERBOSE/MEMORY(23044): 9715624

 05-25 09:53:54.044: VERBOSE/FreeMEMORY(23044): 61528

The free memory seems quite small? Is this cutting close to the bone?

trincot
  • 317,000
  • 35
  • 244
  • 286
  • Android recommends 9-patch images for backgrounds, have you tried that? – omermuhammed May 25 '11 at 01:00
  • The documentation says 9patch is used for stretching bitmaps in Views. Im drawing mine on a canvas. –  May 25 '11 at 01:14
  • Instead of using one large bitmap, have you tried tiling the image to create the background? I have seen it used in game canvases before. – omermuhammed May 25 '11 at 01:16
  • 1
    Instead of trying to create an humongous image, create smaller tiles and display only the ones that you need to at one time. You can only allocate 16 MB of memory so you'll run out of memory pretty quickly with big images. – dmon May 25 '11 at 01:30

2 Answers2

1

re size your image using sampling and store images in file in sdcard it would be better

ud_an
  • 4,939
  • 4
  • 27
  • 43
1

If you are shrinking bitmap then also first it gets loaded into memory and eventually gives you error.

You need to use something called as BitmapFactory.Options which precalulates before loading the bitmap.

Refere to this question's maximum voted answer.

Community
  • 1
  • 1
Harshad
  • 7,904
  • 3
  • 24
  • 42