【问题标题】:MapView keeping Context cause memory leakMapView 保持 Context 导致内存泄漏
【发布时间】:2016-12-12 09:01:52
【问题描述】:

我正在使用 MapView 版本。 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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 2012-01-01
      • 2015-07-06
      • 2014-06-07
      • 2013-11-20
      • 2011-10-28
      • 2016-01-18
      相关资源
      最近更新 更多