Bitmap size exceeds VM budget for game development

I am developing a game on android. Like a tower defense. I use a surface view. I use some kind of image as a bitmap. (Help, thayet, buttons, backgrounds, efects vs.) Now the images are almost 5-6 mb. And I get this error when I run my game:

Bitmap size exceeds VM budget

The 19464192-byte external allocation is too large for this process.

I invoke such images

BitmapFactory.decodeResource(res, id) 

and I put it in an array. I can not scale images. I use all of them. I tried this

 options.inPurgeable=true; 

and it works, but the image loads very slowly. I load a sprite with this, and when it loads, I get very very low fps.

What can I do?

+6
source share
2 answers

I also had this problem; there really is no solution but to reduce the number / size of bitmaps that you uploaded immediately. Some older Android devices only allocate 16 MB per heap for your entire application, and bitmaps are stored in memory without compression after they are downloaded, so it’s easy to exceed 16 MB with large backgrounds, etc. (854x480, 32-bit 1.6 MB uncompressed bitmap.)

In my game, I managed to get around this only by loading the bitmaps that I was going to use at the current level (for example, I have one Bitmap object for the background, which is reloaded from the resources every time it changes, and not supporting several bitmaps in memory . I just maintain an int that keeps track of which resource I have currently loaded.)

Your sprite sheet is huge, so I think you're right that you need to reduce the size of your animations. Alternatively, loading from resources is pretty fast, so you can get away with doing something like loading an animation strip for the current direction of the symbol and slightly pause it when it rotates, when you replace it with a new animation strip. This can get complicated.

In addition, I highly recommend testing your application on an emulator with a bunch of VMs set to 16 MB to ensure that the problem is fixed for all devices. (The emulator usually has a default value of 24 MB, so it’s easy for it to be unverified and create several 1-star reviews after the release.)

+2
source

I am not a game, but I would like to think that I know Android enough.

Uploading images in size will almost certainly result in errors. Why images, file size?

Example: http://p-xr.com/android-tutorial-how-to-paint-animate-loop-and-remove-a-sprite/ . If you notice that he has a sprite with an explosion of only ~ 200 KB. Even a more detailed image does not take much more space.

OK some suggestions:

  • Are you loading all your sprites on one sheet or each sprite in a separate file? If they are all on one, I would separate them.

  • Reduce the resolution of the images, the Android device is portable and some of them have a low resolution screen. For example, HTC Wildfire has a resolution of 240x320 (LDPI device) and a fairly general device. You did not specify image sizes, so we cannot be sure that this is practical.

Finally; I'm not a programmer, but I found this lesson (part of the same series) quite enlightened - http://p-xr.com/android-tutorial-2d-canvas-graphics/ . I wonder if you are using a template that is not suitable for Android, however without code I can not say.

Correctly, something is not the topic, but it is worth noting ...

People value the power of the species. Although there is some logic to using SurfaceView, standard viewing will do quite a lot on its own. A SurfaceView most often requires the execution of the main thread (which you will have to configure yourself) to make it work. A View however calls onDraw (), which can be used in various ways, including the postinvalidate() method (see What does postInvalidate () do? ).

In any case, it might be worth checking out this tutorial http://mindtherobot.com/blog/272/android-custom-ui-making-a-vintage-thermometer/ . Personally, this was a great example of a user view and what you can do with them. I rewrote several sections and made a pocket watch app.

0
source

Source: https://habr.com/ru/post/912138/


All Articles