tree: 0e207394e1ca77c00099f101b6da744173eea584
  1. configs/
  2. images/
  3. samples/
  4. app-code.md
  5. concepts.md
  6. GEMINI.md
  7. java-memory.md
  8. meminfo_sample.txt
  9. native-memory.md
  10. OWNERS
  11. procstats_sample.txt
  12. README.md
  13. showmap_sample.txt
  14. system-wide-memory.md
  15. threads.md
  16. tools-overview.md
guides/memory/README.md

Understanding and Troubleshooting Android Memory

Memory is a critical and finite resource on Android devices. As an Android platform developer or OEM, understanding how memory works at every level—from the Java runtime down to the Linux kernel—is essential for building a performant and stable system.

This guide provides a comprehensive overview of Android memory architecture, the tools available for analysis, and hands-on exercises to help you identify and resolve memory-related issues.

Objectives

After completing this guide, you will be able to:

  • Explain the fundamental concepts of Android memory, including the Zygote process model and memory-mapped files.
  • Differentiate between Resident Set Size (RSS), Proportional Set Size (PSS), and Unique Set Size (USS).
  • Use standard command-line tools like dumpsys meminfo and showmap to quickly assess memory usage.
  • Capture and analyze Java heap dumps using AHAT to find leaks and excessive allocations.
  • Use Perfetto and heapprofd to profile both Java and native memory across the entire system.
  • Understand system-wide memory pressure indicators like Pressure Stall Information (PSI) and monitor Low Memory Killer (LMK) activity.

Guide Structure

This guide is organized into the following sections, designed to be read sequentially:

  1. Fundamental Concepts: The core technical principles of Android memory.
  2. Quick Assessment Tools: Using meminfo, showmap, and procstats for rapid triage.
  3. Analyzing Java Memory: Deep dives into the Java heap with AHAT.
  4. Analyzing Native Memory: Profiling C/C++ memory with Perfetto and heapprofd.
  5. App Code is Memory: Understanding how class loading and dex bloat impact RAM.
  6. Threads and Memory: Analyzing the cost of native thread stacks.
  7. System-wide Troubleshooting: Monitoring the kernel, LMK, and system pressure.

Getting Started: Device Setup

To follow the hands-on exercises in this guide, you should have an Android device with root access connected to your development machine.

If you are using acloud:

You can create a virtual Cuttlefish device suitable for testing:

source build/envsetup.sh
lunch aosp_cf_arm64_phone-trunk_staging-userdebug
acloud create --build-target aosp_cf_arm64_phone-trunk_staging-userdebug --local-image

If you are using a physical device: Ensure your device is running a userdebug build, and enable USB debugging.

Ensure Root Access: Most of the tools used in this guide require elevated privileges. Verify your device is connected and rooted:

adb root
adb wait-for-device

Start Here: Fundamental Concepts