Share

engineering

2min read

How to take a screenshot with 50KB memory left

How to take a screenshot with 50KB memory left

Taking a screenshot on Android is quite a trivial task. There are numerous methods and you can find a lot of code samples on the Internet. The problem is that each of them requires at least as much memory as Android needs to store the image before saving it to the sdcard. While developing Apphance for Android it quickly became apparent that we need a better approach. Especially when we have to take a screenshot in case of frequent crashes caused by OutOfMemoryError.

Our method is very simple - we split the screen into parts. Then create the screenshot by drawing the screen in bit-by-bit using a small, pre-allocated Canvas object. When we started working on a prototype, we thought that it will be extremely slow due to multiple draw calls. The results showed something different. The code is very simple:

View view = getWindow().getDecorView().getRootView();
int width = view.getWidth(), height = view.getHeight();
while (true) {
    try {
        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        for (int y = 0; y < view.getHeight() / height; y++) {
            for (int x = 0; x < view.getWidth() / width; x++) {
                canvas.save();
                canvas.translate(-x * width, -y * height);
                view.draw(canvas);
                canvas.restore();
            }
        }
        bitmap.recycle();
        break;
    } catch (OutOfMemoryError e) {
        if (width > height) {
            width /= 2;
        } else {
            height /= 2;
        }
    }
}

In a loop we’re trying to allocate required amount of memory. If it’s not possible, we split the image in half and try once again. Then we draw small parts onto the bitmap.

Taking a 1-part screenshot takes 170ms on Galaxy Nexus. 2-part takes 70ms and 32-part only 50ms. Why?

  • Allocating a 3.5MB, continuous block of memory takes a lot of time.
  • Each view, which would be drawn outside the screenshot can be quickly detected and rejected.
  • Drawing cache helps a lot.

The results are very promising. Using this method we can take a screenshot using ~50KB of memory, which is very little for managed language. The only thing left is how to merge the parts. During the merging we have to allocate a block of memory for the screenshot. The bitmap will be used in another Activity. Theoretically Android should free the memory used by the previous Activity and give us enough space so that restoring complete bitmap wouldn’t be a problem. Unfortunately the os frees memory with a delay and we cannot ever be sure when it happens.

We can pass a screenshot as a simple structure:

public class Screenshot implements Serializable {
    List<File> files;
    int width, height;
}

Such lightweight object can be passed just through the Intent. In the next Activity we will just have to read all parts, allocate enough memory for the screenshot and draw all the images on the screenshot bitmap in a loop. But, we still have an OutOfMemoryError. Apparently the solution is very simple. Each process shares memory between Activities, but if we don’t want to wait for freeing memory, we can run an another process. We just have to add a simple flag to the Activity declaration in the manifest. In such way we can take a screenshot and pass it on without worrying about lack of memory in our Activity.

Share

Marcin

Senior Software Engineer

Did you enjoy the read?

If you have any questions, don’t hesitate to ask!

Did you enjoy the read?

If you have any questions, don’t hesitate to ask!