我正在使用 MapView 版本。 10.0.1。我遇到了内存泄漏 MapView 持有 Activity 上下文。
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
最佳答案
泄漏很可能来自谷歌地图继续跟踪您的当前位置(如果您已设置)。所以将以下内容添加到您的 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();
}
关于android - MapView保持上下文导致内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41097269/