Memory leak in Android apps is a nightmare. And Memory leak in iOS apps is a sleep killer. 
Tags: android, ios, productivity
Published: 2020-10-28 02:55:06

If you are a mobile app developer, one of the most troubling bugs you might have come across will be memory leaks. Detecting memory leaks in live mobile apps, and identifying where the leak is happening is always a challenge.

# The Out of Memory error
Let's try to visualise what you will be doing when your live app reports an 'Out of Memory' error. OOM is usually caused by a memory leak, or abnormal memory usage triggered by not reusing objects or because a huge object like an image or data set being loaded into the memory.

For abnormal memory usage, stack trace could help in identifying the place of occurrence. But if it is caused by memory leak, the stack trace associated will not help much.

# Debugging memory leak
So if you are an android app developer, you will use _Android studio memory profiler_ or _Leak Canary SDK_ and try to run the possible user flows to detect the location of memory leak. Leak Canary SDK can detect the exact leaking object, but that being a heavy operation, it is not advisable to include this SDK in production apps. It is recommended to use this SDK only for debugging purposes. And if you are an iOS developer, _Xcode Instruments_ provide Memory Profiler instrument which can be used. Also Facebook has provided tools like _FB Memory Profiler_, _FBAllocationTracker_ and _FBRetainCycleDetector_ for iOS, but again they are advisable only to be used in debug mode. But here too, you need to run all possible permutations and combinations of user flows to identify the leak.

And we all know how time consuming this is.

# Data to identify the root cause
Now if the report from live app had provided a chronologically ordered user flow and lifecycle events, along with the Activity or Service in case of Android app, and ViewController in case of iOS app, that would have helped you understand where exactly you should look at to identify the root cause. You would have saved much time in fixing the leak.

# Collecting data
The only SDK / framework in market that effectively detects memory leaks in live mobile apps is **Finotes** ([https://finotes.com](finotes.com)). The Finotes Android SDK identifies the Activity or Service where the leak is happening. The Finotes iOS framework identifies the ViewController where leak is detected. Also it provides a list of lifecycle events and user flow events along with timestamps. These data will help the developer to reproduce the leaks faster using memory profilers. Finotes in debug mode will detect the exact leaking object as well.

So if you are having nightmares or losing sleep due to memory leaks in your apps, try Finotes.

# References
1. [https://developer.android.com/studio/profile/memory-profiler](https://developer.android.com/studio/profile/memory-profiler)
2. [https://developer.apple.com/documentation/xcode/improving_your_app_s_performance/reducing_your_app_s_memory_use/gathering_information_about_memory_use](https://developer.apple.com/documentation/xcode/improving_your_app_s_performance/reducing_your_app_s_memory_use/gathering_information_about_memory_use)
3. [https://github.com/facebook/FBMemoryProfiler](https://github.com/facebook/FBMemoryProfiler)
4. [https://github.com/facebook/FBRetainCycleDetector](https://github.com/facebook/FBRetainCycleDetector)
5. [https://github.com/facebookarchive/FBAllocationTracker](https://github.com/facebookarchive/FBAllocationTracker)
6. [https://square.github.io/leakcanary/](https://square.github.io/leakcanary/)
7. [https://www.blog.finotes.com/post/how-to-detect-memory-leaks-in-android](https://www.blog.finotes.com/post/how-to-detect-memory-leaks-in-android)
8. [https://www.blog.finotes.com/post/detect-memory-leaks-in-ios-objective-c-swift](https://www.blog.finotes.com/post/detect-memory-leaks-in-ios-objective-c-swift)