开发者

Android Out of Memory Error - Bitmap is too big

开发者 https://www.devze.com 2023-03-08 00:17 出处:网络
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 wan开发者_如何

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 wan开发者_如何学运维t 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?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消