Android Maps V2 内存泄漏 LocationClientHelper

标签 android google-maps memory-leaks google-maps-android-api-2 supportmapfragment

我们正在尝试追踪我们 Android 应用程序中 GoogleMap 上发生的内存泄漏,该泄漏在大约 40-50 次设备旋转后以 OOM 结束。该 map 设置了大约 3500 个标记。

该应用的 minSDK 为 9,因此使用 V4 支持库中的 SupportMapFragment。

我们尝试了多种方法,包括:

  • 缓存 LatLng 的
  • 缓存相机更新
  • 从 map 上移除标记
  • 从 map 中移除监听器
  • 移除所有监听器、标记等,这样我们就只有一个普通 map
  • 更新 Google Play 服务库
  • 更新支持库

分析 MAT 中的内存转储表明我们积累了很多实例 com.google.android.gms.location.internal.LocationClientHelper$ListenerTransport 我们不知道它们来自哪里。

有人知道内存泄漏的原因吗?

以下代码已经删除了所有标记和监听器,但仍然存在泄漏。首先是基类:

public abstract class BaseMapFragment extends Fragment {

public static final int MENU_ITEM_ID_SEARCH= 102;
public static final int MENU_ITEM_ID_SHOW_LIST= 100;
public static final int ZOOM_LEVEL_DEFAULT= 14;

private static final String SAVED_INSTANCE_LATITUDE= "savedLatitude";
private static final String SAVED_INSTANCE_LONGITUDE= "savedLongitutde";
private static final String SAVED_INSTANCE_ZOOM= "savedZoom";

protected static final String CLASSTAG= BaseMapFragment.class.getSimpleName();

private GoogleMap mMap;
private CameraUpdate mResumeCameraUpdate= null;
private double mSavedLatitude;
private double mSavedLongitude;
private float mSavedZoom;
private static View mView;

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    if (mMap != null) {
        outState.putDouble(SAVED_INSTANCE_LATITUDE, mMap.getCameraPosition().target.latitude);
        outState.putDouble(SAVED_INSTANCE_LONGITUDE, mMap.getCameraPosition().target.longitude);
        outState.putFloat(SAVED_INSTANCE_ZOOM, mMap.getCameraPosition().zoom);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    if (savedInstanceState != null) {
        mSavedLatitude= savedInstanceState.getDouble(SAVED_INSTANCE_LATITUDE, Constants.EXTRA_VALUE_NONE);
        mSavedLongitude= savedInstanceState.getDouble(SAVED_INSTANCE_LONGITUDE, Constants.EXTRA_VALUE_NONE);
        mSavedZoom= savedInstanceState.getFloat(SAVED_INSTANCE_ZOOM, Constants.EXTRA_VALUE_NONE);
    }

    if (mView != null) {
        ViewGroup parent= (ViewGroup) mView.getParent();
        if (parent != null)
            parent.removeView(mView);
    }
    try {
        mView= inflater.inflate(R.layout.map_layout, container, false);
    } catch (InflateException e) {
        /* map is already there, just return view as it is */
    }
    return mView;
}

protected GoogleMap initializeMap() {
    if (mMap != null) {
        if (mSavedLatitude != Constants.EXTRA_VALUE_NONE && mSavedLatitude != 0.0) {
            mResumeCameraUpdate= Context.getCamUpdate(mSavedZoom, mSavedLatitude, mSavedLongitude);
        } else {
            mResumeCameraUpdate= Context.getCamUpdate(mMap.getCameraPosition().zoom, mMap.getCameraPosition().target.latitude, mMap.getCameraPosition().target.longitude);
        }
    }

    SupportMapFragment mapFragment= (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
    if (mapFragment == null) {
        mapFragment= (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
        if (mapFragment == null) {
            MapsInitializer.initialize(getActivity());
            mapFragment= SupportMapFragment.newInstance();
            mMap= mapFragment.getMap();
        } else {
            mMap= mapFragment.getMap();
        }
    } else {
        mMap= mapFragment.getMap();
    }

    // check if map is created successfully or not
    if (mMap == null) {
        Toast.makeText(getActivity().getApplicationContext(), R.string.map_create_unable, Toast.LENGTH_SHORT).show();
    } else {
        mMap.setMyLocationEnabled(true);
        mMap.setOnMyLocationButtonClickListener(new OnMyLocationButtonClickListener() {
            @Override
            public boolean onMyLocationButtonClick() {
                if (mMap.getMyLocation() != null) {
                    CameraUpdate newLatLngZoom= Context.getCamUpdate(ZOOM_LEVEL_DEFAULT, mMap.getMyLocation());
                    mMap.animateCamera(newLatLngZoom);
                } else {
                    Toast.makeText(getActivity().getApplicationContext(), R.string.map_location_services_disabled, Toast.LENGTH_SHORT).show();
                }
                return true;
            }
        });

    }
    return mMap;
}

}

子类

public class MySupportMapFragment extends BaseMapFragment {

private LinearLayout mStaoButtonsLayout;
private ToggleButton mStaoButton;
private ToggleButton mGasStaoButton;

private Boolean mInitialLocationChange;
private CameraUpdate mResumeCameraUpdate;
private GoogleMap mMap;
private double mBundleLatitude;
private double mBundleLongitude;


@Override
public void addRequiredModelClasses(LinkedHashSet<Class<? extends ComergeModel<?>>> set) {
    set.add(AboModel.class);
    set.add(StationModel.class);
    super.addRequiredModelClasses(set);
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putDouble(BUNDLE_EXTRA_CENTER_LATITUDE, mBundleLatitude);
    outState.putDouble(BUNDLE_EXTRA_CENTER_LONGITUDE, mBundleLongitude);        
}


@Override
public void onActivityCreated(final Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    setHasOptionsMenu(showSearchButton());

    final StationModel stationModel= getContext().getModel(StationModel.class);

    mStaoButtonsLayout= (LinearLayout) getActivity().findViewById(R.id.mapStaoButtons);
    mStaoButtonsLayout.setVisibility(View.VISIBLE);
    mStaoButton= (ToggleButton) mStaoButtonsLayout.findViewById(R.id.staoButton);
    mStaoButton.setChecked(stationModel.isStationButtonChecked());
    mGasStaoButton= (ToggleButton) mStaoButtonsLayout.findViewById(R.id.gasStaoButton);
    mGasStaoButton.setChecked(stationModel.isGasStationButtonChecked());

    mMap= initializeMap();
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    addSearchButton(menu);
}

}

最佳答案

我之前遇到过类似的问题。我添加了以下代码来解决我的问题:

@Override 
public void onDestroy() {
     if (mMap != null) {
         mMap.setMyLocationEnabled(false);
     } 
 }

LocationClientHelper$ListenerTransport 似乎与 setMyLocationEnabled() 有关。我不得不注销一些回调以防止内存泄漏。

关于Android Maps V2 内存泄漏 LocationClientHelper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31726621/

相关文章:

google-maps - Google map 无法在 https ://中工作

r - 意外的 R 内存管理行为

android - IllegalStateException 映射大小不应为 0

android - 在 Recycler View 中显示来自 firebase 的特定数据

java - 无法启动 Activity ComponentInfo{Activity} : android. view.InflateException:二进制 XML 文件行 #12:膨胀类 fragment 时出错

javascript - 谷歌地图 API : Get url for current view

memory - IntPtr 导致内存泄漏?

c# - 打开MediaPlayer-线程中未处理事件

java - 在类(class)中添加广告 Admob 并从另一个 Activity/类(class)调用

java - 无论时区如何,以 UTC 格式获取设备时间