Skip to content Skip to sidebar Skip to footer

Android: Extreme Lag When Using High-resolution Drawables

I decided to create a splashscreen at the beginning of my app. I created a 1600x900 image and used it as a drawable. When I ran my app, it seemed like every action bears a 1 second

Solution 1:

The lag you are experiencing is probably due to Garbage Collection or Heap size increase.

Your image is 1600*900 = 1440000px. Each pixel is has an in-memory size of four byte - one per color (RGB) and one for transparency (A). We can therefore calculate: 1440000 * 4B = 5760000B so around 5.7 MB allocated on the heap for the byte-array alone. But it's a drawable, and has some more fields than the byte-array, so you end up with a little more.

Add Activities, Strings, other images and resources and it's much higher.

Now, whenever you create a new object the VM tries to delete some unused objects (Garbage collection) causing a small lag. If it cannot free enough memory the Heap will increase (also causing a small lag).

You are probably facing one of these issues, so what can you do about it?

You didn't write it, but I assume that the splash screen is not the only Activity of your App. The problem is that the splash screen stays active in the background. You can't really do anything about that, but you can at least remove the drawable manually.

Viewwindow = getWindow().getDecorView();
if (window.getBackground() != null) {
    window.getBackground().setCallback(null);
    window.setBackground(null);
}

In general, it's a better idea to not use large images in xml attributes. It's much more memory efficient to create the bitmap yourself in size you really need (not all devices have a 1600*900 resolution).

Android gives you the BitmapFactory class to do this. Check out Pre-scaling Bitmaps and Most memory efficient way to resize bitmaps on android? for more info on the whole topic.

Post a Comment for "Android: Extreme Lag When Using High-resolution Drawables"