androiddevnotes / awesome-android-learning-resources

👓 A curated list of awesome android learning resources for android app developers.
Creative Commons Zero v1.0 Universal
1.64k stars 258 forks source link

Memory Leak Learning Resources #57

Open androiddevnotes opened 2 years ago

androiddevnotes commented 2 years ago

(Draft)

Setup

Getting Started - LeakCanary

Tutorial

Practical demonstration of identifying Memory Leaks in Mozilla Firefox (Fenix on GitHub)

Shows how to read the "leak trace", how to identify memory leaks.

The Sandbox: LeakCanary - YouTube

General info and what's new in LeakCanary 2

Ask the Expert #2: LeakCanary 2.x is out, track your memory leaks - YouTube

Tools

The Android Profiler  |  Android Developers - Only Memory section of Profiler is relevant.

LeakCanary

Code

Reproducible Sample of Memory Leak

Run the app in Android Studio.

maxkohne/Android-Memory-Leaks: This is an example of how memory leaks happen with fragments in Android. It also has a solution to the problem.

Notes

Frequent, consistent Garbage Collection in Android Studio Profiler (Memory) is not good.

CoordinatorLayout leaked does not necessarily mean that you should look into the XML code of CoordinatorLayout. Refer to the The Sandbox: LeakCanary - YouTube to see how to read such leak trace.

At times, when using the Android Studio profiler (memory), you may want to disable or comment out LeakCanary dependency from your build gradle file. (Not necessarily).

Different Types of Memory Leak and Reference

The list is not exhaustive.

Note, not all code is tested. But helpful references.

RecyclerView adapter null

fenix/AddonsManagementFragment.kt at 7070bb5607f5ac9fd0825ab21e7abba675f98bdc · mozilla-mobile/fenix

How to Fix Memory Leak in RecyclerView (LeakCanary) - YouTube

AlertDialog Memory Leak

florisboard/TypingFragment.kt at 9a146ba2f0040e8cf1652cd21dd358268cda3759 · florisboard/florisboard

android - Leak Canary detects memory leaks for TabLayout with ViewPager2 - Stack Overflow

Using backing field pattern (when necessary)

The Sandbox: LeakCanary - YouTube

Need to close actionMode if contextual actionMode is showing.

food-recipes/FavoriteRecipesFragment.kt at 3a66c9b796d99cd5926f7743f2699c470d5acbb4 · ZattWine/food-recipes

setOnRefreshListener null

SkateShare/FeedFragment.kt at ab2d4499b841b6c8cb6ea2f3d261570e8f05b336 · avhagedorn/SkateShare

searchView.setOnQueryTextListener(null)

GreedyNewsApp/NewsFragment.kt at e3b290a07d53d83a5b5a8dd5fb60e9a6fcf8bd04 · ShobhitCoder/GreedyNewsApp

drawerHelper.removeDrawerListener(listener)

Tv/MeFragment.kt at 3c22f2baad651c3df53b3615fd6fc28263621a42 · bytebyte6/Tv

Dismiss Dialog (set to null) and Input Method Manager

NfcDemo/MainActivity.kt at 2cce6dc82fddc18c1c00aa04cdee25b300ecb77a · ewgcat/NfcDemo

viewpager2

ViewPager2 internally uses RecyclerView I think, so use the same technique to prevent memory leaks as you would use for RecyclerView adapter.

ViewPager2 holds on to FragmentStateAdapter after detach [151212195] - Visible to Public - Issue Tracker

NavigationComponents-Tutorials/ViewPagerContainerFragment.kt at c2fcaf175fbd99183d424d1281a47275e1101f85 · SmartToolFactory/NavigationComponents-Tutorials

Do it before super.onDestroyView (!?)

Not sure if before or after nulling the view is preferred. People do either ways.

architecture-components-samples/InflateFragment.kt at main · android/architecture-components-samples

Avoid references to the Fragment's Views after onDestroyView() by ianhanniballake · Pull Request #785 · android/architecture-components-samples