Understanding Mobile Memory: Stack vs Heap, Common Issues, and Testing with dumpsys and LeakCanary
This article explains the differences between stack and heap memory on Android devices, outlines typical memory problems such as high usage, leaks, and overflow, and demonstrates practical testing methods using the dumpsys meminfo command and the LeakCanary library with code examples.
Mobile memory usage is a critical factor for app performance, and developers need to monitor and diagnose memory issues both during development and after release.
Stack vs. Heap : The stack is managed by the operating system and stores function addresses, parameters, and local variables, requiring relatively little space. The heap is controlled by the program via new , free , delete , and can grow to hundreds of megabytes or several gigabytes, making it prone to misuse.
Common memory problems include excessive memory consumption compared to competitors, memory leaks where allocated memory is not released, and memory overflow (OOM) when the requested memory exceeds system limits.
Testing method 01 – Using dumpsys : Connect the device and run dumpsys meminfo / to obtain detailed memory statistics such as PSS (actual physical memory), private dirty, heap size, heap allocated, and heap free. You can also query total memory for a process with dumpsys meminfo | find "Packagename" .
Testing method 02 – LeakCanary : LeakCanary is an efficient tool for detecting memory leaks. Add the appropriate dependency in build.gradle :
dependencies{ debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3' releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3' }
Initialize it in your Application class:
public class ExampleApplication extends Application { @Override public void onCreate() { super.onCreate(); LeakCanary.install(this); } }
When a leak is detected, a toast appears and the LeakCanary UI shows a detailed leak trace, highlighting the reference chain that prevents garbage collection (e.g., an inner class retaining a reference to its outer MainActivity ).
These techniques help developers identify and resolve memory issues, leading to more stable and efficient Android applications.
360 Quality & Efficiency
360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.