Eugene Stepanov
Eugene Stepanov

Reputation: 164

MapView keeping Context cause memory leak

I'm using MapView ver. 10.0.1. I am getting a memory leak MapView is holds activity context.
LeakCanary trace:

com.demo.app.ui.main.MainActivity has leaked:
GC ROOT wl.a
references oo.a
references maps.ad.L.g
references maps.ad.V.c
references maps.D.i.a
references maps.D.p.mParent
references android.widget.FrameLayout.mParent
references com.google.android.gms.maps.MapView.mContext
leaks com.demo.app.ui.main.MainActivity instance

Upvotes: 1

Views: 1012

Answers (1)

Chris Hare
Chris Hare

Reputation: 291

The leak is most likely coming from google maps continuing to track your current location (if you have it set). So add the following to your onDestroy()

@Override
public void onDestroy() {

    if (mMapView != null) {
        mMapView.onDestroy();
    }

    //Clean up resources from google map to prevent memory leaks. 
    //Stop tracking current location
    if(mGoogleMap != null) {
        mGoogleMap.setMyLocationEnabled(false);
    }
    super.onDestroy();
}

Upvotes: 4

Related Questions