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
-
-
-
Communicate with Snackbar messages6m 15s
-
Manage view components with Butter Knife6m 41s
-
Create a reusable dialog class9m 22s
-
Convert a layout to ConstraintLayout7m 13s
-
Manage global data with singletons5m 37s
-
Convert image files to WEBP format6m 5s
-
Create an Espresso user interface test5m 18s
-
Create classes for Android with Kotlin5m 12s
-
Run background tasks in intent services8m 51s
-
Send broadcast messages with EventBus8m 44s
-
Handle events with Java 8 syntax5m 51s
-
Improve forms with design layouts7m 55s
-
Display draggable rows in a list of data8m 1s
-
Manage persistent data with Realm10m 23s
-
Play an audio file from assets8m 56s
-
Send an SMS message from an app7m 56s
-
Get a list of audio files on a device8m 32s
-
Play sounds with SoundPool8m 45s
-
Display the build date of an APK file6m 47s
-
Build Kotlin classes with properties7m 32s
-
Manage versioning with Gradle variables6m 8s
-
Use reporting tools from the Google Play Console4m 44s
-
Make web service calls with Retrofit10m 39s
-
Make asynchronous calls with Retrofit4m 6s
-
Send request parameters with Retrofit4m
-
Upgrade a project to Android Studio 3.04m 47s
-
Clean up calls to findViewById()2m 58s
-
Create adaptive launcher icons5m 47s
-
Use XML and downloadable fonts6m 40s
-
Explore a device's file system4m 55s
-
Profile an app's memory usage3m 52s
-
Manage an SQLite database with Room9m 17s
-
Manage background threads with Executors5m 32s
-
Track vital stats in a published app3m 4s
-
Inspect database files with DB Browser4m 41s
-
Execute tasks with JobScheduler7m 29s
-
Create Room entity classes in Kotlin8m 22s
-
Create Room DAO classes in Kotlin4m 13s
-
Upgrade to Android Studio 3.13m 22s
-
Convert a project to use Kotlin3m 20s
-
Handle events with Kotlin lambdas6m 13s
-
Manage menus with when3m 44s
-
Define global constants with Kotlin6m 12s
-
Use Kotlin data and enum classes5m 24s
-
Create inheritance hierarchies in Kotlin6m 8s
-
Manage data collections in Kotlin5m 45s
-
Sort data collections with Kotlin5m 33s
-
Filter data collections with Kotlin3m 52s
-
Create Kotlin extension functions2m 31s
-
Use support functions from Android KTX4m 30s
-
Use virtual devices with Google Play3m 50s
-