Similar to JVM, Dalvik Runtime on Android has Garbage Collector (GC) to destroy unused objects after they are out of scope, this doesn't mean we can ignore memory management. There are other ways that memory can be leaked logically within Java, so we have to be aware of these situation.
When developing Midtrans Android SDK, we faced a lot of such issues where continous use would bloat the memory usage. Here is how we fixed some of them
How memory leak occur
- Context (activity) reference to another class
- Static Context (activity)
- Static View
- Unmanaged Asynchronous Process
- Sensor Manager
Identifying memory leak
A big warning sign to lookout for is the ever increasing memory usage in your app (look at the yellow circle on the picture below).
Wherever we see little drops in the graph, a GC event has occurred, removing unused objects and freeing space on the heap (shown by red circle in the picture above). But if the graph stays constant and growing then it could be a memory leak
Fixing Memory Leaks
Memory leaks can be very difficult to track it. It takes a bit of practice and lots of trial and error. Fortunately android Studio was improved a lot lately, with more and more tools to help us find and analyze performance issues. The Memory tab on the Android profiler window, will show us the amount of data being allocated on the heap over time.
In order to investigate what’s currently allocated in our heap, we need to take snapshot memory process at the time, we can use the heap dump button on left android monitor menu. This will take a snapshot of what’s currently allocated in the heap, and will show it in a report screen inside Android Studio
On the left tab, we see a list of the instances in the heap, grouped by their class name. For each one, there’s the amount of objects allocated, the size of these instances (Shallow size) and the size these objects are retaining in memory. in the middle tab show us about instance of class.
Android provide Task analyzer to help us find the leak per activity. you just need to run the task analyzer and if there are leak suspect, they will be shown in analysis results, and the detail will be shown in the bottom tab. the leak generally marked by blue name. as you can see it caused by instance in
There are several to find memory leak on android apps.
MAT (Eclipse MAT) is a more advanced tool is the Eclipse Memory Analyzer Tool . you can use it to analyze the hprof of android apps. to using it, we need hprof file from android. and then import it to MAT by select menu file -> open heap dump -> leak suspect report -> finish. after that you need select to select dominator tree on overview tab.
In order to make you easier to analyze you need to filter the file by your package name. for detail information
Another great tool is a library called Leak Canary, which tracks the objects and makes sure they aren’t leaked. If they are, you’ll get a notification to let you know what happened and where. for using it you just need to install it to your apps by this way. below the sample leak canary notification when memory leak occur.