How to Detect and Diagnose Android Memory Leaks with MAT, AspectJ, and UIAutomator

This guide explains practical techniques for spotting basic memory leaks, using the MAT tool to analyze heap dumps, comparing hprof files, and implementing daily leak monitoring in Android apps through AspectJ instrumentation, MLD utilities, and UIAutomator automation.

Tencent TDS Service
Tencent TDS Service
Tencent TDS Service
How to Detect and Diagnose Android Memory Leaks with MAT, AspectJ, and UIAutomator

1. Basic Memory Leak Test

Repeatedly perform the suspect workflow while monitoring memory curves with a profiling tool; a continuously rising trend that does not drop after returning indicates a leak. This simple, high‑value method quickly reveals the most obvious leaks.

2. Using MAT for Memory Analysis

2.1 Inspect Total Heap Size

In the Devices view, select the target app, click “Update Heap”, open the Heap view, and press “Cause GC”. The Heap view shows a "data object" type with a "Total Size" column representing the total memory of Java objects. Stable Total Size values suggest no leak, while a steadily increasing value after GC indicates retained objects.

2.2 Locate Leak Sources via hprof

When a leak is suspected, dump the heap to an hprof file and analyze it in MAT:

Identify the leaked class.

Find external objects holding references to that class.

Examine the GC paths of those holder objects.

Verify each GC path for abnormal retention.

Typical findings may reveal utility classes (e.g., antiRadiationUtil) holding a reference to MainActivity, preventing its release.

2.3 Compare Pre‑ and Post‑Operation hprof Files

Open two heap dumps, add their histograms to the Compare Basket, and run the comparison. The diff highlights object types whose instance counts grew, pinpointing the data objects responsible for the leak.

3. Mobile Guard Daily Memory Leak Monitoring

3.1 AspectJ Instrumentation

An Ant script injects MLD monitoring code via AspectJ, allowing the source code and monitoring logic to be packaged separately. Instrumented builds generate log files that serve as the basis for later analysis.

3.2 MLD Core Monitoring Logic

The MLD tool adds a phantom (virtual) reference to each monitored object. When the garbage collector prepares to reclaim the object, the phantom reference is enqueued, enabling the tool to count unreclaimed objects against a threshold.

PhantomReference + ReferenceQueue = detection of objects about to be reclaimed.

3.3 UIAutomator for Automated Interaction

UIAutomator drives repetitive UI actions automatically, eliminating manual effort. Test cases are defined in configuration files, ensuring consistent coverage across app versions.

The combined approach—AspectJ‑based instrumentation, MLD phantom‑reference monitoring, and UIAutomator automation—provides a robust daily leak detection pipeline for activities, services, and views.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Androidmemory leakaspectjMATUIAutomator
Tencent TDS Service
Written by

Tencent TDS Service

TDS Service offers client and web front‑end developers and operators an intelligent low‑code platform, cross‑platform development framework, universal release platform, runtime container engine, monitoring and analysis platform, and a security‑privacy compliance suite.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.