MAT: Getting Setup
Profiling memory usage of an Android application can really help pinpoint performance issues. Your app might be janky or laggy when scrolling, or you may run into the dreaded application OutOfMemoryError. In an effort to tackle these issues I’ve decided to start learning how to correctly profile an Android app and get some meaningful results to hopefully remedy any issues.
OS: Yosemite 10.10.1
Android Studio: Version 1.2
Device: OnePlus One running Lollipop nightly’s
You do not need Eclipse to use the MAT tool. Simply download the version suitable for your OS.
Create a heap dump file
Open Android Studio and select the option from the toolbar to open the Android Debug Monitor. The icon is the little green Android guy:
Next, open your app on your device and use it like a typical user would for a few seconds.
In the Android Debug Monitor, select your device and your application process.
Click the “Update Heap” button above the process list:
Open the “Heap” tab and then hit the “Cause GC” button:
Once the GC has occurred (you will see data in the heap tab updated), click the “Dump HPROF file” option. This may take a few seconds so be patient. Save the .hprof file and remember its location.
Using the HPROF file
Ok, we’re nearly there. We have HPROF file but before we can use it in MAT, we need to convert it into a compatible format. Luckily, the Android SDK provides a tool called “hprof-conv” in the platform-tools folder. So open up a terminal and type the following command:
hprof-conv ~/Documents/com.test.profiling.hprof ~/Documents/converted.com.test.profiling.hprof
If you get a “command not found” error in the terminal, you need to add the Android SDK to your path (details here). Also, remember to change the HPROF file path in the command if you saved the it in a different location.
We’re finally ready to use MAT! Open up MAT and select the option to open a new heap dump. Find your converted HPROF file (“converted.com.test.profiling.hprof”) and open it. You will be given a choice of reports, choose any for now and we’ll look at each option in-depth later.