安卓地图V2内存泄漏问题LocationClientHelper

5
我们正在尝试跟踪我们的Android应用程序中GoogleMap上发生的内存泄漏,大约在40-50次设备旋转后会导致OOM。地图上设置了约3500个标记。
该应用程序具有minSDK 9,并因此使用来自V4支持库的SupportMapFragment。
我们尝试了多种方法,包括:
- 缓存LatLng - 缓存CameraUpdates - 从地图中删除标记 - 从地图中删除侦听器 - 删除所有侦听器、标记等,以便我们只有一个简单的地图 - 更新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);
}

}

1个回答

9

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

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

看起来LocationClientHelper$ListenerTransport与setMyLocationEnabled()有关。为了防止内存泄漏,我不得不注销一些回调函数。


1
你,我的朋友,应该得到一枚奖章!谢谢!此外,我们将地图上设置的监听器置空,似乎解决了问题! - SOERGI
1
哥们,你太棒了!这个我会给你数百个赞的!我们也会向谷歌报告,因为这真的很奇怪! - byemute
1
谢谢你分享这个!这终于解决了泄漏问题!你应该得到我们的一个拥抱 :) - byemute
不要忘记在结尾处调用super.onDetach();否则会出现异常。 - fjs
@fjs 你的意思是什么?我们也应该把这段代码放到 onDetach 中吗?如果是的话,你说得完全正确。 - byemute
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接