Chapter 5. Memory Performance

At the end of Chapter 4, we examined an issue where processes in the app blocked the UI thread, preventing the screen to update. In this chapter, we’ll look at how to measure and better understand how your app uses memory. Memory leaks are a major cause of crashes on Android, and using the tools discussed in this chapter to diagnose issues will help you prevent such leaks. Let’s kick off the discussion with memory management and tips to optimize, and then in the second half of the chapter, we’ll cover how to minimize the CPU usage of your app.

Android Memory: How It Works

Before we can discuss how to improve the memory efficiency of your Android app, we need to start with the basics on how Android handles memory management. Once we get a solid background on that, we can understand some of the pitfalls and how to resolve them. To introduce some of the basic terms, let’s get some simple information from your Android device.

As you may be aware, the Java runtime on your Android device (whether Dalvik or ART) is a memory-managed environment. The runtime typically handles all memory allocations and cleanup (garbage collection). This does simplify the development of your app by abstracting those details from your code, but there are important considerations to take while building your app to ensure that the memory management works correctly.

Let’s start with a quick set of definitions on the types of memory that are utilized by Android apps.

Shared Versus ...

Get High Performance Android Apps now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.