Android Performance

Android Memory Optimization (2) - Advanced MAT Usage

Word count: 657Reading time: 4 min
2015/04/11
loading

This is the second article in our MAT series, focusing on advanced techniques for analyzing memory issues in Java and Android applications.

  1. Android Memory Optimization (1) - Introduction to MAT
  2. Android Memory Optimization (2) - Advanced MAT Usage
  3. Android Memory Optimization (3) - Viewing Original Bitmaps in MAT

Characteristics of Java Memory Leaks

  • Main features: Reachable but Useless.
  • Useless: Objects created but not released after they are no longer needed.
  • Inefficient: Re-creating new objects for tasks where existing ones could be reused.

Advanced MAT Techniques

Dumping Memory with Android Studio

Modern versions of Android Studio make capturing heap dumps easy:
Android Studio Memory Profiler

Capture the hprof file, right-click to convert it to standard format, and open it in MAT.

Tip: Before capturing a heap dump, always manually click the Initiate GC button. This ensures your snapshot doesn’t include unreachable objects that are just waiting for a collection cycle.
Manually Trigger GC

Unreachable Objects

Unreachable objects are those that can be reclaimed by the GC but haven’t been because no collection has occurred yet. MAT can show these if the dump was taken without a preceding GC.
Unreachable Objects Histogram

When you click Calculate Retained Size, you’ll notice that unreachable objects have a Retained Heap value of 0, which is expected.
Unreachable Objects with 0 Retained Heap

Histogram Analysis

The Histogram is specialized for checking instance counts, particularly for your own classes.

  • Sort by Percentage to quickly find the biggest memory consumers.
  • Group by different dimensions: Group by class, Group by class loader, or Group by package. Dominator Tree also supports these group types.
  • Comparing two Histograms over time is the best way to find leaking objects.
  • Histogram vs. Dominator Tree: Histogram looks at memory from the perspective of classes, while Dominator Tree looks from the perspective of object instances. Dominator Tree is superior for understanding complex reference chains.

Histogram Group by Package

By grouping by package, you can isolate your own code and identify classes that are either reachable-but-useless or being recreated unnecessarily.

Thread Information

MAT can display active threads:
Thread Overview

This provides insights into:

  1. Threads that might be causing memory issues due to their stack size or held references.
  2. An unusually high number of threads, which itself can be a memory/performance issue.

Context Menus and Help

Right-clicking any item in MAT reveals a wealth of analysis options:
Context Menu Options

Use the Search Queries option (usually at the bottom) to look up the exact meaning of these commands. Often, these commands are just pre-configured SQL-like queries.

Key queries include:

  • List objects -> with incoming references: Find what this object refers to.
  • List objects -> with outgoing references: Find what objects refer to this one.
  • Path To GC Roots -> exclude all phantom/weak/soft etc. references: Show the path from a GC Root via Strong References only. Since soft/weak/phantom references can be cleared by the JVM, a leak is almost always caused by a persistent strong reference.
  • Merge Shortest path to GC root: Find the common path from a GC Root to a group of objects.

Debugging Bitmaps

In many Android apps, Bitmaps occupy the majority of the heap, especially on 2K/4K screens where a single Bitmap can exceed 20MB.

MAT allows you to export the raw byte[] data of a Bitmap to a file, which can then be viewed with third-party tools. This is a game-changer for identifying which images are leaking. See Viewing Original Bitmaps in MAT for the full workflow.

Debugging ArrayLists

Checking content in an ArrayList via standard outgoing references can be tedious as it exposes the internal array structure.
ArrayList Internal Structure

Instead, use MAT’s Extract List Values feature for a much more intuitive view:
Extract List Values Result

Big Drops In Dominator Tree

The “Big Drops In Dominator Tree” tool identifies accumulation points. It highlights objects where there is a large difference between the retained size of a parent and its children. These are typically places where many small objects are being aggregated under one parent object, indicating a potential hotspot.

Big Drops In Dominator Tree

About Me && Blog

(Links and introduction)

CATALOG
  1. 1. Characteristics of Java Memory Leaks
  2. 2. Advanced MAT Techniques
    1. 2.1. Dumping Memory with Android Studio
    2. 2.2. Unreachable Objects
    3. 2.3. Histogram Analysis
    4. 2.4. Thread Information
    5. 2.5. Context Menus and Help
    6. 2.6. Debugging Bitmaps
    7. 2.7. Debugging ArrayLists
    8. 2.8. Big Drops In Dominator Tree
  3. 3. About Me && Blog