SupportMapFragment is leaking Memory

Viewed 756

I am trying the new version of GoogleMap

com.google.android.libraries.maps:maps:3.1.0-beta

But it seems to leak memory when I come back to my previous Activity

I'm using Fragment inside my Activity

Here is the code:

XML

<androidx.fragment.app.FragmentContainerView
        android:id="@+id/map"
        android:name="com.google.android.libraries.maps.SupportMapFragment"
        android:layout_width="0dp"
        android:layout_height="220dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:useViewLifecycle="true"
        tools:context=".OutputScreen"/>

Java Code

    public class OutputScreen extends FragmentActivity implements OnMapReadyCallback{
    
    
    private GoogleMap map;
    private SupportMapFragment mapFragment;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            
            //setting layout
    
            mapFragment = (SupportMapFragment) 
            getSupportFragmentManager().findFragmentById(R.id.map);
            mapFragment.getMapAsync(this);
        }

        @Override
        public void onMapReady(GoogleMap googleMap) {
          map = googleMap;
        }

        @Override
        protected void onDestroy() {
          if (map != null){
              map.clear();
              map.setMyLocationEnabled(false)
              map = null;
          }
        }
    }

LeakCanary Report

   ┬───
│ GC Root: System class
│
├─ android.provider.FontsContract class
│    Leaking: NO (Application↓ is not leaking and a class is never leaking)
│    ↓ static FontsContract.sContext
├─ android.app.Application instance
│    Leaking: NO (Application is a singleton)
│    Application does not wrap an activity context
│    ↓ Application.mActivityLifecycleCallbacks
│                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~
├─ java.util.ArrayList instance
│    Leaking: UNKNOWN
│    ↓ ArrayList.elementData
│                ~~~~~~~~~~~
├─ java.lang.Object[] array
│    Leaking: UNKNOWN
│    ↓ Object[].[5]
│               ~~~
├─ com.google.android.libraries.maps.dj.zzf instance
│    Leaking: UNKNOWN
│    ↓ zzf.zza
│          ~~~
├─ com.google.android.libraries.maps.dj.zzd instance
│    Leaking: UNKNOWN
│    ↓ zzd.zzd
│          ~~~
├─ com.google.android.libraries.maps.bm.zzu instance
│    Leaking: UNKNOWN
│    ↓ zzu.zzd
│          ~~~
├─ com.google.android.libraries.maps.kf.zzab instance
│    Leaking: UNKNOWN
│    ↓ zzab.zzh
│           ~~~
├─ com.google.android.libraries.maps.kf.zzd instance
│    Leaking: UNKNOWN
│    ↓ zzd.zzi
│          ~~~
├─ com.google.android.libraries.maps.ed.zzau instance
│    Leaking: YES (View detached and has parent)
│    mContext instance of android.app.Application, not wrapping activity
│    View#mParent is set
│    View#mAttachInfo is null (view detached)
│    View.mWindowAttachCount = 1
│    ↓ zzau.mParent
├─ android.widget.FrameLayout instance
│    Leaking: YES (zzau↑ is leaking and View detached and has parent)
│    mContext instance of android.app.Application, not wrapping activity
│    View#mParent is set
│    View#mAttachInfo is null (view detached)
│    View.mWindowAttachCount = 1
│    ↓ FrameLayout.mParent
╰→ android.widget.FrameLayout instance
​     Leaking: YES (ObjectWatcher was watching this because com.google.android.libraries.maps.SupportMapFragment received Fragment#onDestroyView() callback (references to its views should be cleared to prevent leaks) and View.mContext references a destroyed activity)
​     

I looked other stackoverflow answers but didn't found any proper solution.

1 Answers

The issue is that the SupportMapFragment is incorporated into your Fragment container view, but the ondestroy is not being called. You need to manually invoke this.

In your OutputScreen class under the onDestroy() method, you need to call the following:

mapFragment.onDestroy()  // call this in your activity's onDestroy
Related