保持上下文的MapView导致内存泄漏

时间:2016-12-12 09:01:52

标签: android google-maps memory-leaks android-mapview leakcanary

我正在使用MapView ver。 10.0.1。我得到内存泄漏MapView持有活动上下文 LeakCanary追踪:

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

1 个答案:

答案 0 :(得分:3)

泄漏最有可能来自谷歌地图继续跟踪您当前的位置(如果您已设置)。因此,将以下内容添加到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();
}