Heads up... You're reading this book for free, with parts of this chapter shown beyond this point astext.
Memory is a storage space in computers or mobile devices, just like the human brain! This is a place where data or programs are kept on a temporary or permanent basis to be processed.
Any smartphone app like PodPlay keeps occupying your device’s memory as long as it’s running in the foreground or background. It’s important to ensure your app is functional with minimal memory usage and leaves enough room for the Android OS and other apps to operate correctly. The Memory Profiler is a tool built within Android Studio that helps you understand, analyze and optimize your app’s memory usage.
In this chapter, you’ll use the Memory Profiler and learn:
- The basics of memory management in Android.
- Tracking memory allocations.
- About memory leaks and how to prevent them.
Before moving any further, you might want to recall Android’s memory management basics.
Android Memory Management 101
Android is a managed-memory environment. The Android Runtime (ART) and Dalvik Virtual Machine use memory paging and mapping techniques to manage memory for your Android device. Below are the key elements of Android memory management:
- Memory Allocation: This is the process of reserving memory for your app’s objects and processes.
Stack Memory: Android uses this for static memory allocation. Local variables, references to objects and primitive types are common examples of static memory. Each thread has its separate stack, organized in a LIFO order (last in, first out). The unified stack size on Android Runtime for both Java and C++ is around 1MB, which is relatively small compared to Heap memory.
StackOverflowErroroccurs when an app hits its stack memory limit.
Heap: Android uses this for dynamic memory allocation. The heap is a piece of memory where the system allocates Java/Kotlin objects in no particular order. Android runtime limits the heap size for each running application to ensure a smooth multitasking experience. There is a variation of the heap size limit among devices and it depends on how much RAM a device has. If your app hits this heap limit and tries to allocate more memory,
OutOfMemoryErrorwill occur, and will terminate your app.
This is what the memory allocation looks like for a simple application:
Garbage Collection: Garbage Collection is an action taken by the system to avoid memory-related issues such as
OutOfMemoryError. When the system determines that any program or app no longer uses a piece of memory, it frees the memory back to the heap, without user intervention. The system conducts:
- Search for data objects that are no longer referenced and you can’t access them.
- Reclamation of the memory occupied by those objects.
This process cleans up and reclaims unused memory.
Why You Should Profile Memory
Garbage collection is a necessary process for memory management, but if it happens too often, it can negatively impact your app’s performance. The system has to pause the app’s code to let the Garbage Collector do its job. Normally, this process can be quite fast and imperceivable from the user’s perspective.
Overview of Memory Profiler
Android Profiler is a combination of various tools that provide real-time information about your app, such as memory allocation and resource usage.
Running Memory Profiler
To see overall memory usage by PodPlay at any point since the app launched, put the cursor above the event timeline. You’ll see the memory count of your app segmented into several categories as follows:
Tracking Memory Allocations
The memory allocations panel helps you see each object and JNI reference in your memory:
Recording Memory Allocation
You’ll now see how to analyze when you use PodPlay and objects the app creates in memory. Open the Memory Profiler, select Record Java / Kotlin allocations and click Record:
Memory Allocation Table
The table displays a list of allocated objects, grouped by class name and sorted by their heap count as it’s shown below:
Forcing Garbage Collection
At this point, you might want to see how garbage collection impacts memory allocation. To force a garbage collection, click the Trash icon highlighted below:
Improving Profiling Performance
To improve performance during profiling, the Memory Profiler samples memory allocations periodically. You can change this by using the Allocation Tracking dropdown beside the Trash icon shown above.
Memory Churn And Memory Leaks
You just learned to force garbage collection by yourself while using the Memory Profiler. Android Runtime performs garbage collection periodically in a typical scenario, but what if there’s a case that “forces” the system to do it?
Identifying and Improving Memory Performance: Detecting Memory Leaks
You can analyze the current state of the memory by performing a heap dump. It shows which objects in your app are using memory when you capture them. After a user session, a heap dump can help identify memory leaks by showing objects still in memory that should no longer be there.
Capturing a Heap Dump
To capture a heap dump, perform these actions as you’ve previously completed:
Saving and Importing a Heap Dump
After you capture a heap dump, the data is visible in the Memory Profiler as long as the profiler is running. You lose the heap dump as soon as you exit the profiling session.
Fixing a Memory Leak
You might be yearning to fix the memory leak you’ve seen during heap dump. Don’t worry, you can easily filter on profiling data that the Memory Profiler thinks might induce memory leaks in your app.
Memory Profiling: Tips & Tricks
Memory Leak is very common since developers can’t see it happening while developing the app. That’s why memory profiling is crucial.
- Memory Profiling keeps app performing at its max. Profiling your app at different stages of development can result in finding memory leaks early.
- Memory Profiler offers a complete toolset to view memory allocations, method stack-trace and memory leaks if any.
- Capturing and analyzing heap dumps frequently helps to understand memory allocation and improvement points and find memory leaks.
Where to Go From Here?
Though memory management is a complex and vast topic, you learned a lot about memory allocations, garbage collection, and analyzing memory allocation from a heap dump of fixing memory leaks! To learn more on this topic, check out the tutorials and videos below: