开发者

Android Development: Bitmaps on surfaceview leak Memory or too big?

开发者 https://www.devze.com 2023-03-26 06:40 出处:网络
My game uses surfaceview(I know I should use GL). I draw alot of bitmaps to my game character world controll so on. And i run into this when I opened my LogDog:

My game uses surfaceview(I know I should use GL). I draw alot of bitmaps to my game character world controll so on. And i run into this when I opened my LogDog:

08-05 10:17:29.151: ERROR/dalvikvm(24048): Out of memory: Heap Size=5379KB, Allocated=2735KB, Bitmap Size=20576KB, Limit=32768KB

I dont know if it is leak or what.

My Allocation Tracker shows:

like 30:

138 96  char[]  9   android.content.res.AssetManage开发者_如何学Pythonr    getCookieName   

Then Tons of:

32  80  android.graphics.BitmapFactory$Options  9   android.graphics.BitmapFactory  decodeResource  

And last: Like 30:

141 56  android.graphics.Bitmap 9   android.graphics.BitmapFactory  nativeDecodeAsset   

And also some more of simular ones.

Here is some code that I think drains my memory:

player = BitmapFactory.decodeResource(getResources(), R.raw.ghostright);
        world = BitmapFactory.decodeResource(getResources(), R.raw.lvl2);
        thumb = BitmapFactory.decodeResource(getResources(), R.raw.thumb);

resized = Bitmap.createScaledBitmap(player, width/10, width/6, false);
         player = resized;


         resized = Bitmap.createScaledBitmap(world, height*10, height, false);
         world = resized;


         resized = Bitmap.createScaledBitmap(thumb, height/6, height/6, false);
         thumb = resized;

I heard that I should use resycle but I dont know where because I always use the bitmaps

//Simon PS: I really need help -.-


I use a lot of bitmaps on SurfaceView too and don't have this problem. When it comes to animated sprites, you can use a sprite sheet rather than load them individually frame by frame.

You don't need to use the reference "resized" you can just say:

player = Bitmap.createScaledBitmap(player, width/10, width/6, true);

the old bitmap will lose its reference and be collected by GC. Note that I placed TRUE for bitmap filtering when rescaling to make a better quality.

On some devices onSizeChanged can happen two times which may resize bitmaps twice, if that is where you are doing your scaling.

The format of loaded bitmaps does metter whether it is ARGB_4444 or ARGB_8888 etc So you may need to explore this option and if you can use a format which requires less memory yet it has a good enough quality for your game. Of course the rule is not to load images into memory bigger than they are needed and when they are needed.


It dosen't have to be a memory leak, it could just be that you have so large bitmaps that they want to allocate to much memory. Here's a good method to determine how much memory a bitmap is going to take: W*H*8. So if you have a bitmap that's 300*300 px it's 300*300*8 = 720 kb.

Figure out how much allocated heap you have at any given time and see if it increases with time even though you know that you're not allocating new bitmaps. If so, then yes, you have a memory leak. If, however, your app crashes right on startup, then you are probably just exceeding heap limit.

0

精彩评论

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