Memory leak in FirebaseInAppMessagingDisplay

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

Yes

[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
 ├─ com.example.app.App
 │    Leaking: NO (Application is a singleton)
 │    ↓ App.mActivityLifecycleCallbacks
 │          ~~~~~~~~~~~~~~~~~~~~~~~~~~~
 ├─ java.util.ArrayList
 │    Leaking: UNKNOWN
 │    ↓ ArrayList.elementData
 │                ~~~~~~~~~~~
 ├─ java.lang.Object[]
 │    Leaking: UNKNOWN
 │    ↓ array Object[].[3]
 │                     ~~~
 ├─ com.google.firebase.inappmessaging.display.FirebaseInAppMessagingDisplay
 │    Leaking: UNKNOWN
 │    ↓ FirebaseInAppMessagingDisplay.firebaseInAppMessagingDisplay
 │                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 ├─ com.google.firebase.inappmessaging.display.FirebaseInAppMessagingDisplay$1
 │    Leaking: UNKNOWN
 │    Anonymous class implementing com.google.firebase.inappmessaging.FirebaseInAppMessagingDisplay
 │    ↓ FirebaseInAppMessagingDisplay$1.val$activity
 │                                      ~~~~~~~~~~~~
 ╰→ com.example.app.ui.main.MainActivity
 ​     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. 🙏