From the course: Android Development Tips

Profile an app's memory usage - Android Tutorial

From the course: Android Development Tips

Profile an app's memory usage

- [Instructor] Android Studio 3 includes new versions of the profiler tools. I'm going to talk specifically about the memory profiler. One of the most common debugging tasks that Android developers have is to figure out where the memory is going, what operations are using up memory, and whether you're correctly releasing memory so you're not leaking resources. The Android memory profiler is part of the Android profiler tab. The first time you click it on Windows you might see a firewall prompt asking you if you want to allow traffic between the profiler and your device. Accept the offer, and then you'll be able to see the information I'm showing here. There are three profilers available, for CPU, memory, and network usage. I'm going to be focused on the memory profiler, so I'll click that. I'm going to go to my device, where I'm already running my application. I'm going to open up my application list, and then I'll clear the application from memory. You'll see that the memory profiler basically goes off a cliff at that point. Now I'll come back to Android Studio, and I'll run the application again. After a moment the app starts up on the device, and the profiler returns to its base state. I'll come back here and click on memory again. Now I can see exactly what's happening on the device. Notice that my baseline usage is a little bit under 100k. Watch what happens when I click Run Code. I have a bunch of code in the application that's going to chew up memory like crazy. It's creating instances of a POJO class, a data object, and adding references to each of those instances to a list, an array list. It's never releasing any of the references. As a result, my memory usage climbs and it keeps on climbing for as long as I let this infinite loop continue. Each trashcan icon that you see here represents a garbage collection event triggered by the application framework and the virtual machine. Now I'm going to stop the data. I'll click Clear Log, and that executes some code that releases all the references to the data objects and releases the reference to the list. The data usage evens out, but there's still a lot of data being used by the application. I'll come back to Android Studio. I'll click on this trashcan icon on the toolbar. That forces garbage collection right away. You'll see that the memory usage drops almost Immediately. It goes down to that baseline of a little less than 100k. That's in the allocated section. But there's still data being used in various areas of the device. You can explore the code that I created, or you can create your own. In order to demonstrate this, I created an application that chews up memory in a background thread. It uses a runnable object, and then starts that runnable with a thread object. There's an infinite loop. Each time through the loop it adds 100 items to the list, and then sends an event using Event Bus to the foreground thread. Down here, the on message event method receives that event object and displays the message it receives. That's how I'm communicating from the background thread to the foreground thread. You can create your own code to try eating up memory and seeing what happens, or perhaps you have your own application you'd like to test out. All you need to do is open the application in debug mode on a virtual or physical device, open the Android profiler tab, and go to the memory profiler. Watch what happens as you try out your application's functionality.

Contents