Memory leak in FirebaseInAppMessagingDisplay

[READ] Step 1: Are you in the right place?


[REQUIRED] Step 2: Describe your environment

  • Android Studio version: 3.5 Beta 3
  • Firebase Component: firebase-inappmessaging-display
  • Component version: 17.1.1

[REQUIRED] Step 3: Describe the problem

Steps to reproduce:

Install LeakCanary and Firebase In App Messaging in the project. Launch the app. Wait for the leak notification to appear.

LeakCanary is reporting a memory leak for FirebaseInAppMessagingDisplay. Here is part of the leak report:

 ├─ android.provider.FontsContract
 │    Leaking: NO (it's a GC root and a class is never leaking)
 │    ↓ static FontsContract.sContext
 │    Leaking: NO (Application is a singleton)
 │    ↓ App.mActivityLifecycleCallbacks
 │          ~~~~~~~~~~~~~~~~~~~~~~~~~~~
 ├─ java.util.ArrayList
 │    Leaking: UNKNOWN
 │    ↓ ArrayList.elementData
 │                ~~~~~~~~~~~
 ├─ java.lang.Object[]
 │    Leaking: UNKNOWN
 │    ↓ array Object[].[3]
 │                     ~~~
 │    Leaking: UNKNOWN
 │    ↓ FirebaseInAppMessagingDisplay.firebaseInAppMessagingDisplay
 │                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 │    Leaking: UNKNOWN
 │    Anonymous class implementing
 │    ↓ FirebaseInAppMessagingDisplay$1.val$activity
 │                                      ~~~~~~~~~~~~
 ​     Leaking: YES (RefWatcher was watching this and MainActivity#mDestroyed is true)
 (removed the rest of the leak dump)

Looking into the Firebase code myself, I think this is the cause of the leak. An anonymous class is created which references the activity parameter. This class is then saved in the variable firebaseInAppMessagingDisplay, which is never cleared when the activity is destroyed.

3 thoughts on “Memory leak in FirebaseInAppMessagingDisplay

  1. This has quite some impact on all apps using FirebaseInAppMessaging. Would be great to get some feedback whether someone is actively looking into it. 🙏