Android Maps V2内存泄漏位置ClientHelper

Android Maps V2内存泄漏位置ClientHelper,android,google-maps,memory-leaks,google-maps-android-api-2,supportmapfragment,Android,Google Maps,Memory Leaks,Google Maps Android Api 2,Supportmapfragment,我们正试图在Android应用程序中追踪谷歌地图上发生的内存泄漏,在大约40-50次设备轮换后,该程序在OOM中结束。这张地图大约有3500个标记 该应用程序的minSDK为9,因此使用V4支持库中的SupportMapFragment 我们尝试了多种方法,包括: 缓存板条的 缓存摄影机更新 从地图中删除标记 从映射中删除侦听器 删除所有侦听器、标记等,这样我们就有了一个简单的映射 更新Google Play服务库 更新支持库 分析MAT中的内存转储表明,我们积累了大量的内存实例 com.g

我们正试图在Android应用程序中追踪谷歌地图上发生的内存泄漏,在大约40-50次设备轮换后,该程序在OOM中结束。这张地图大约有3500个标记

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

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

  • 缓存板条的
  • 缓存摄影机更新
  • 从地图中删除标记
  • 从映射中删除侦听器
  • 删除所有侦听器、标记等,这样我们就有了一个简单的映射
  • 更新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);
}

}
公共类MySupportMapFragment扩展了BaseMapFragment{
私人线路布局MSTAO按钮布局;
私人切换按钮mStaoButton;
专用切换按钮MGASSTAO按钮;
私有布尔最小分配更改;
私人相机更新mResumeCameraUpdate;
私有谷歌地图;
私人双重关系;
私人双重财产;
@凌驾

public void addRequiredModelClasses(LinkedHashSet我以前遇到过类似的问题。我添加了以下代码来解决我的问题:

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

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

这可能是谷歌类中的一个内存泄漏吗?谷歌地图问题已揭晓:你我的朋友,应该得到一枚奖章!谢谢!此外,我们取消了地图上设置的侦听器,似乎已经解决了问题!哥们,你太棒了!会给你几百个对这件事的投票数太多了!我们还不如向谷歌报告这件事,因为这真的很奇怪!谢谢你分享这件事!这终于解决了这个漏洞!你应该得到我们的拥抱:)感谢您向Google报告此问题。希望他们能够解决或至少在wiki上注意到此问题。同时感谢darijan解决了我糟糕的英语问题。:)不要忘了在最后调用super.onDetach();否则您将遇到异常。。