Mobile Development 14 min read

Sieve – Android Memory Analysis System: Background, Implementation, and Results

This article introduces Sieve, an internal Android memory analysis platform that parses Hprof files, builds dominator trees, calculates retained sizes, and visualizes reference chains to help developers identify OOM causes, while discussing the limitations of existing tools and implementation details.

JD Retail Technology
JD Retail Technology
JD Retail Technology
Sieve – Android Memory Analysis System: Background, Implementation, and Results

The article starts by describing the persistent challenge of memory problems on Android, where leaks or unreasonable memory usage lead to performance degradation, OOM crashes, and poor user experience. It points out that existing tools such as LeakCanary and MAT have limitations: LeakCanary only reports objects that developers deem should not exist, while MAT is not well‑suited for Android-specific objects like large Bitmaps.

To address these issues, JD's internal project Sieve – Android Memory Analysis System is introduced. The tool accepts an Hprof heap dump, parses it, and generates a detailed analysis report that includes visualizations of activity/fragment memory usage, bitmap allocations, object reference paths, and the top‑30 classes by retained size.

Implementation Overview

The implementation consists of four main steps:

Parsing the Hprof file to extract class information, stack frames, heap objects, and especially GC Roots.

Generating a dominator tree (支配树) to identify which objects dominate others.

Calculating Retained Size for each object by summing the shallow sizes of all nodes it dominates.

Constructing reference chains from suspected leaking objects back to GC Roots.

GC Roots are classified into types such as Stack Local, Thread, JNI Local, JNI Global, and references from the non‑collecting generation. Understanding GC Roots is crucial because they represent the set of objects that will never be reclaimed.

The article explains key concepts:

Shallow Size : the memory occupied by the object itself.

Retained Size : the total memory that would be freed if the object and all objects it dominates were reclaimed.

For example, the following Java class is used to illustrate shallow size calculation:

public class TestA {
    int a;
    TestB b;
}

The dominator tree is built using a topological order for DAGs; when cycles exist, an iterative approach is applied until the direct dominator of each node converges. The article notes that the Lengauer‑Tarjan algorithm offers a more efficient solution for graphs with cycles.

Retained Size is computed by traversing the dominator tree and adding each node's shallow size to all its ancestors. Special handling is required for Bitmaps because their mBuffer can be promoted to a GC Root, breaking the expected parent‑child relationship; the tool therefore maps mBuffer back to its owning Bitmap during size aggregation.

Reference chain construction uses a breadth‑first search from a suspected leaking object toward GC Roots, recording the shortest paths. The resulting paths are stored in an adjacency‑list structure, exemplified by the following class definition:

public final class PathsFromGCRootsTree {
    private String description;
    private ArrayList
inboundTrees;
}

Finally, the article discusses practical challenges when dumping Hprof files on devices close to OOM: Debug.dumpHprof() triggers a GC pause, and the resulting files can be hundreds of megabytes, potentially causing the analysis process to OOM as well. Strategies such as threshold‑based instance pruning are mentioned, though they may lead to incomplete reference graphs.

Overall, Sieve provides a comprehensive, developer‑focused solution for Android memory profiling, combining low‑level heap parsing, dominator analysis, retained‑size metrics, and visual reference tracing.

AndroidMemory Profilingdominator-treeRetained SizeHprofGC Roots
JD Retail Technology
Written by

JD Retail Technology

Official platform of JD Retail Technology, delivering insightful R&D news and a deep look into the lives and work of technologists.

0 followers
Reader feedback

How this landed with the community

login 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.