Connection and Disconnection

Technology is developing rapidly, and the connections between people seem to be getting closer and closer. We can’t deny that right now we have the most advanced methods of real time communication in…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Overdraw in Android

While debugging an old app I wrote a couple of years ago, I found that I was wasting a significant amount of GPU with no explanation. In this post, I’ll try to explain what Overdraw is and how you can detect if your app is having this problem.

In context

The Graphics processing unit (or GPU) is a physical part of a computer that generates, manipulates and displays the graphics to be seeing by the user. All the applications running in a computer with a Graphical User Interface (or GUI) uses the GPU to display buttons, messages, etcetera. In this case, any Android device shares the same architecture as a computer and therefore also uses a GPU.

In Android Studio, you can monitor a lot of possible variables in a device or emulator. With this tool, you can track memory leaks, unclosed connections to Internet resources or open database connections. You can also monitor the usage of the GPU. The more UI elements are shown in the screen, this usage can grow.

If we see our UI as a set of layers, each one with its own UI elements, Android implements an algorithm to decide which parts of the layers to show in front and which ones should be covered by front UI layers. The Algorithm used to define this is called Painter’s Algorithm.

This algorithm might decide between UI elements to show but sometimes it fails to decide certain parts of the UI elements that share the same importance and layer. Although this happens in a small bunch of pixels in the whole screen, this might confuse the GPU that will try to paint multiple times the same pixels with two or more different colors (one color per UI element). So this confusion in a running app generates a GPU leakage which is known as Overdraw.

Common and Dangerous Practices that might lead to an Overdraw

If your layout XML file has a at least a couple of overlapped elements such as Linear Layouts or Coordinator Layouts, you are in the dangerous zone. In your code, if you can’t properly manage the visibility of the items, all the overlapped elements to show will have the same importance to the algorithm and will share the same layer.

Also, if you run layout background animations, this might trigger a lot of GPU usage. Android OS limits it. If the app freezes continually or is kinda slow, it can be that the GPU usage grew exponentially due to Overdraw since the pixels are updating all the time and somehow it confuses the GPU to calculate its importance to show it on screen.

A very common issue but not to easy to track

Without monitoring, you’ll only see your app is slow to interact with like pressing a button and seeing its actions after a couple of seconds. You’ll blame the device or the internet connection before thinking it is the GPU.

Fortunately you can monitor each event and user interaction with Android Studio to catch if this GPU usage grows. If so, it might be an Overdraw and there are certain techniques to fix your app from it.

In my personal case, I had a couple of overlapped Linear Layouts. One containing buttons for certain actions and the other one with warning messages. I just learned that I was showing both layouts by default and based in some variables I set the visibility of each one. According to the monitor, the GPU usage grew a lot only at the beginning of my activity and every time I showed the warnings layout. I could easily fixed it by setting the right logic too set the default visibility and also setting the elevation attribute of the warnings layout to 2 so the Android algorithm will know that the actions layout was in the background of the warning’s. This helped the algorithm to decide which layout has the heuristics to be on top.

To learn more

Add a comment

Related posts:

What Our Community Partners Are Saying

Every time they come I am impressed by how serious they take the experience. Their curiosity to explore and engage with others in a new community is worth seeing. I personally get a lot out of them…

A Workflow Engine within your Code

To follow up on the announcement of Zenaton launch, I wanted to present a more detailed explanation of the problem that Zenaton aims to solve and the solution that we’re proposing. A startup’s…

How To Make Better Facebook Image Ad Creatives For Dropshipping

Facebook is not an easy channel to navigate for advertisers. You have to compete with your fellow dropshippers and your target audience’s family and friends for attention. Why would someone check out…