MapView v2 保持上下文
MapView v2 keeping Context around
当使用来自最新 google 地图 API 的 MapView 时,我遇到了内存泄漏,因为 MapView 占用了我的 activity。
我使用了 Leak Canary 并且有这个痕迹
D/LeakCanary:* GC ROOT com.google.android.gms.location.internal.t.a
D/LeakCanary﹕ * 参考文献com.google.android.gms.location.internal.s.a
D/LeakCanary﹕ * 引用com.google.maps.api.android.lib6.d.v.c
D/LeakCanary﹕ * 引用com.google.maps.api.android.lib6.d.aj.b
D/LeakCanary﹕ * 引用 com.google.maps.api.android.lib6.gmm6.c.p.a
D/LeakCanary﹕ * 引用 com.google.maps.api.android.lib6.gmm6.c.y.mParent
D/LeakCanary:*参考文献android.widget.FrameLayout.mParent
D/LeakCanary:*参考文献com.google.android.gms.maps.MapView.mContext
D/LeakCanary:* 泄漏com.myapp.activities.main.AttractionDetailActivity 实例
有人以前看过这个吗?
检查您是否在 onMapReady() 回调中调用 googleMap.setMyLocationEnabled(true)
。
如果是,则应在 onDestroy 中调用 googleMap.setMyLocationEnabled(false)
。
这对我有用:
@Override
public void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
if (mMap != null) {
mMap.setMyLocationEnabled(false);
mMap.clear();
}
}
Just to give another option as this solution didn't work for me.
我发现这个非常有用的线程,它提出了一些与 MapView 内存泄漏相关的解决方法:
https://github.com/googlesamples/android-play-location/issues/26
对我来说,这个线程中最有趣的事情(对我有用)是:
1) Make sure to unregister your callbacks:
if (googleApiClient != null) {
googleApiClient.unregisterConnectionCallbacks(this);
googleApiClient.unregisterConnectionFailedListener(this);
if (googleApiClient.isConnected()) {
LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this);
}
googleApiClient.disconnect();
googleApiClient = null;
}
2) Use a WeakReference for LocationListener
public class WeakLocationListener implements LocationListener {
private final WeakReference<LocationListener> locationListenerRef;
public WeakLocationListener(@NonNull LocationListener locationListener) {
locationListenerRef = new WeakReference<>(locationListener);
}
@Override
public void onLocationChanged(android.location.Location location) {
if (locationListenerRef.get() == null) {
return;
}
locationListenerRef.get().onLocationChanged(location);
}
}
希望对您有所帮助!
当使用来自最新 google 地图 API 的 MapView 时,我遇到了内存泄漏,因为 MapView 占用了我的 activity。
我使用了 Leak Canary 并且有这个痕迹
D/LeakCanary:* GC ROOT com.google.android.gms.location.internal.t.a
D/LeakCanary﹕ * 参考文献com.google.android.gms.location.internal.s.a
D/LeakCanary﹕ * 引用com.google.maps.api.android.lib6.d.v.c
D/LeakCanary﹕ * 引用com.google.maps.api.android.lib6.d.aj.b
D/LeakCanary﹕ * 引用 com.google.maps.api.android.lib6.gmm6.c.p.a
D/LeakCanary﹕ * 引用 com.google.maps.api.android.lib6.gmm6.c.y.mParent
D/LeakCanary:*参考文献android.widget.FrameLayout.mParent
D/LeakCanary:*参考文献com.google.android.gms.maps.MapView.mContext
D/LeakCanary:* 泄漏com.myapp.activities.main.AttractionDetailActivity 实例
有人以前看过这个吗?
检查您是否在 onMapReady() 回调中调用 googleMap.setMyLocationEnabled(true)
。
如果是,则应在 onDestroy 中调用 googleMap.setMyLocationEnabled(false)
。
这对我有用:
@Override
public void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
if (mMap != null) {
mMap.setMyLocationEnabled(false);
mMap.clear();
}
}
Just to give another option as this solution didn't work for me.
我发现这个非常有用的线程,它提出了一些与 MapView 内存泄漏相关的解决方法:
https://github.com/googlesamples/android-play-location/issues/26
对我来说,这个线程中最有趣的事情(对我有用)是:
1) Make sure to unregister your callbacks:
if (googleApiClient != null) {
googleApiClient.unregisterConnectionCallbacks(this);
googleApiClient.unregisterConnectionFailedListener(this);
if (googleApiClient.isConnected()) {
LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this);
}
googleApiClient.disconnect();
googleApiClient = null;
}
2) Use a WeakReference for LocationListener
public class WeakLocationListener implements LocationListener {
private final WeakReference<LocationListener> locationListenerRef;
public WeakLocationListener(@NonNull LocationListener locationListener) {
locationListenerRef = new WeakReference<>(locationListener);
}
@Override
public void onLocationChanged(android.location.Location location) {
if (locationListenerRef.get() == null) {
return;
}
locationListenerRef.get().onLocationChanged(location);
}
}
希望对您有所帮助!