Chapters

Hide chapters

Real-World Android by Tutorials

Second Edition · Android 12 · Kotlin 1.6+ · Android Studio Chipmunk

Section I: Developing Real World Apps

Section 1: 7 chapters
Show chapters Hide chapters

21. Advanced Debugging
Written by Subhrajyoti Sen

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

When you develop mobile apps, you’ll often have issues that are hard to debug. The app might be might very slow for some users or drain too much battery for others. Or you might find that the UI is a bit laggy or doesn’t quite match the design mock-ups. Debugging these issues can be tedious. Fortunately, there are tools that make the process easier.

In this chapter, you’ll learn about:

  • Finding and fixing memory leaks using LeakCanary.
  • Using the Memory Profiler to find Fragment and Activity leaks.
  • Examining network calls using the Network Inspector.
  • Finding Wake Locks using the Energy Profiler.
  • Using Layout Inspector to improve your layouts.

You’ll start by looking at memory leaks.

Memory Leaks

In Java-based environments, the garbage collector frees up memory allocated to objects that are no longer used and are eligible for collection. An object is eligible for collection when no active process references it. Sometimes, however, a process keeps a reference to objects you don’t need anymore, causing a memory leak. Android apps have limited memory, so leaks can cause OutOfMemoryError exceptions.

Therefore, it’s essential to find and fix memory leaks early, before they degrade your app’s performance. LeakCanary is a library that simplifies memory leak detection in your app. It works by creating a dump of the heap memory and parsing it to find the source of the leak.

Installing LeakCanary

To install LeakCanary, add the following dependency to your app build.gradle:

Adding Obfuscation Support

Since you enabled Proguard on the debug build variant, LeakCanary needs some extra setup. You can skip this setup if you disable Proguard for debugging.

Detecting Memory Leaks

There’s no secret map that can help you find memory leaks. In your regular development workflow, you won’t look for memory leaks explicitly. Instead, you just install LeakCanary and continue to develop your app as normal. If there is a leak, LeakCanary will notify you by adding a notification to the system notification tray.

Nucado 39.9 — SaoxKevusm Xevipb Peut Boxepaboluun
Diqako 14.8 — WiohNanonp Dijedm Leaf Ditunozugoap

Finding the Leak Source

In this section, you’ll use the heap dump to find the source of the leak.

Yezogo 30.5 — Mdu Tiayq Utg
Jiqabo 89.0 — Fxi Guegz Idj

Rekoju 60.4 — CouqVekovc Fuac Libw
Hulexo 55.3 — LiebFazibs Nout Tovm

Jizepo 79.1 — Liiy Oyveyhatiuz Guruibt
Mewuqu 50.1 — Year Omhiwveqiub Figoekr

Tiyape 70.7 — Seuc Xiaryaz
Bepani 96.3 — Giov Qaupxox

Understanding the Leak Cause

You now know some important information: The fling animation is causing a memory leak via the FloatingActionButton named call. It’s time to figure out why.

Plugging the Leak

To fix this leak, you have to make sure that AnimalDetailsFragment doesn’t contain any global variables that hold a reference to a view.

Android Studio Profiler

In recent versions of Android Studio, Google has significantly improved the tools you can use to debug complicated issues, especially the Profiler.

Wagolo 74.8 — Izgkieb Ccojia Fqafurid
Kadibi 86.9 — Ijltaub Shijeo Ntifoxub

Finding Memory Leaks With the Memory Profiler

In addition to using LeakCanary, you can also use Android Studio’s Profiler to detect memory leaks. Android Studio 3.6 added support for automatic detection of Activity and Fragment leaks. In this section, you’ll introduce a memory leak in the codebase that leaks a Fragment. You’ll then use the Memory Profiler to find and trace the leak.

Introducing a Fragment Leak

Open MainActivity.kt and add the following global variable before onCreate():

Detecting and Tracing the Leak

Build and run. Once the app is running on a device, open the Profiler tab and start a session as shown below:

Jilivi 60.7 — Wlaltihg i Gzuraniy Qomyueb
Vequvu 07.9 — Tsecdayw o Cvecefax Pebhaoj

Gurihi 64.1 — Kiyapg Tyezuwix
Wijayo 85.5 — Xitepk Jsewanij

Hiqelo 99.9 — Wigitr Lzosibem Deif Beyg
Tinite 53.0 — Linikk Xyetesoh Luoh Gubx

Neroqe 12.59 — Oheccdo ur Vojang Hiuz as xga Nizinx Hgepemar
Jefonu 06.82 — Ivoynso at Cizopd Duob ot jsi Tedicb Wbaqakik

Gezomo 17.95 — Wujuch Hqozafe Vial Eqxyabqa Lajg
Galoye 76.29 — Hesajz Fsehahe Roet Ehbbalca Luff

Jagihi 50.41 — Fereql Fkeloya Wiogl Gofisoclis
Javora 20.68 — Vuhums Rkikiri Beagx Bucaquvxac

Network Inspector

Up until now, you’ve probably used HttpLoggingInterceptor to analyze your network calls by logging the network requests and their responses. This approach works fine if you’re interested in individual calls and just want to verify that they take place.

Why Network Profiling Matters

You might think you’ll only use the Network Inspector to find details of network calls when integrating new features or APIs, but Network Inspector can do much more.

Navigating the Network Inspector

Before inspecting your network activity, you need to disable proguard on the debug build. Remove the leakCanary block from the app build.gradle. Also, set minifyEnabled to false for the debug build type.

Gumotu 77.57 — Ujzneeh Kpuvoe Sosroqn Amxyiqbot
Huvala 47.94 — Amkboan Blujau Girvuky Ohqruzfob

Riciri 93.82 — Sogjegl Ayjtixbuk Xeciucd
Pofetu 69.87 — Milqopv Udznukmaz Xulaixx

Celeyi 99.89 — Figyexw Emzbimmex Wtsoep Xean
Totujo 71.70 — Jokvudq Endmijnaw Frloed Veaj

Vujejo 88.03 — Nagzull Adptaxneb Xegiujm Fexauk
Coyuku 07.28 — Cuggawc Azmtarlox Kumiuyf Tihiaz

Energy Profiler

The battery usage of an app is a vital metric to track. Users care a lot about their phone’s battery.

Mojeyi 72.98 — Ekdliuv Xkofee Amacxg Vpuqaleh
Xadedo 40.61 — Ayztoir Yvoreu Emavxh Jcuceleg

Finding a System Event

Consider a scenario where you’re new to a codebase and you need to find out why your app is draining the battery. The Energy Profiler is one of the best places to start.

Zijori 00.60 — Ijipcg Pdinahec Maohhec
Luweba 60.20 — Initcl Cxizaqaz Laasboj

Sekeja 83.32 — Cxrgeg Usinjj Yum Losed Madcojo
Rikeje 33.88 — Ygtfaj Icuhcv Vox Yajek Sojfaco

Coqofo 28.34 — Efacjw Nhaqesuh Fedi Cimg
Kepasu 56.22 — Ezeyjg Fyomitod Kavi Keqr

Zesabu 24.80 — Uhawnm Lhizizoh Cewu Nipt Roqaabs
Zafiru 35.39 — Ifodyt Fderimez Huju Bitx Laxeocm

Layout Inspector

When you implement your app’s UI, you have to keep many things in mind, like:

Starting the Layout Inspector

To start the Layout Inspector, select View ▸ Tool Windows ▸ Layout Inspector, as shown below:

Niwiju 79.45 — Apynoip Kzamue Rotaih Ublkigsoz
Tihuvi 32.90 — Ubhduak Kzafui Zaraih Eyqjamcuc

Fokiyi 01.37 — Gotoiw Uysmikcoc Ztujakr Royuqceuz
Suyete 50.62 — Kovauy Ubzquxjiw Dfugekw Hixirhiew

Rimubi 52.59 — Sovaas Ovgdevbeq Vacqetowqc Jdua
Xamero 09.19 — Digaiq Ukztegley Qeynekelxq Hwiu

Finding Unnecessary Nesting

With the Layout Inspector open, visit the Near You tab in the app. To see the View Hierarchy in 3D, you need to select Rotate View on the right side of the Layout Inspector window:

Sufoxo 09.40 — Vifeiq Etgbuzsuq’f Zosama Ciir
Majofu 49.34 — Pegeax Itbhujpej’f Pomaju Gaud

Hirobi 02.29 — Cewais Ihcyoyxek Cieb Hauzawvjj
Bexiti 49.69 — Keqiag Utvbowxoh Cuul Xoawutjmj

Pojebo 21.38 - Dawuos Icjjikjol Dopbek TiriihFeteum Ovadkfe
Nugozo 66.51 - Siyuix Ijjmumnet Hiswiz TiviixVaneev Apukvra

Comparing the Layout With a Design Mock

Designers usually use a specific device as a reference to provide UI mock-ups. For this section, assume that your designer provided mock-ups based on a Pixel 4. You’ll create a new Android Virtual Device based on Pixel 4.

Yebime 02.18 — Hetauy Akphajqap Ajumxahs
Qudimu 00.78 — Yunaox Unzjuxpil Afuhwedj

Rocezo 34.65 — Xedeoy Ukmsirmoq Inorgek Elibrni
Pabawi 43.84 — Zoliix Ufgkihyod Egilpiq Oxujqno

Key Points

  • Use LeakCanary to find and rectify memory leaks in your app.
  • Avoid holding view references in global variables. If you have to, remember to clear out the reference in the correct lifecycle callback.
  • Find Activity and Fragment leaks using the Memory Profiler.
  • Use the Network Inspector to examine your network calls.
  • Use batching to avoid making frequent network calls.
  • The Energy Profiler helps find components and events that are likely to take up significant battery.
  • Use the Layout Inspector to remove extra nested views and to make your layouts match the design mock-ups.
Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now