Android MapActivity泄漏在与mNetworkHandler相关的Activity的Context中

时间:2014-10-21 20:02:29

标签: android memory-leaks android-mapview

我无法通过使用MapActivity来查找泄漏的原因。在过去的几个月里,我一直在使用Eclipse内存分析工具来跟踪应用程序中的各种内存泄漏,但我似乎无法找到解释MapActivity的使用方法以及如何正确清理资源的方法

从MAT开始,内存泄漏似乎来自com.google.android.maps.MapActivity的mNetworkHandler,使My Activity的上下文保持活动状态,反之亦然。在此特定屏幕上,泄漏表面将显示每个新的纬度/经度,并将其输入MapActivity。如果我不断地使用相同的坐标进入此屏幕,则泄漏不会增长。

下面你会找到我正在使用的(精简)代码。

public class MyMapActivity extends MapActivity
{
    MapView mMapView;
    List<Overlay> mMapOverlays;

    public void onCreate(Bundle savedInstanceState) {
        mMapView = (MapView) findViewById(R.id.mapview);
        mMapView = new MapView(this, mapApiKey);
        mMapView.setClickable(true);
        setContentView(mMapView);
        mMapView.setBuiltInZoomControls(true);
        mMapOverlays = mMapView.getOverlays();

        mMapView.postDelayed(Timer_Tick, 20000);

    }

    private Runnable Timer_Tick = new Runnable() {
        public void run() {
            if (!mResponseProcessed) {
                UIHelper.showErrorDialogOnGuiThread(mCurrentActivity, "Error!");
            }
        }
    };
    private void showMapObject(String message) {
        final MapController mc = mMapView.getController();
        mc.animateTo(point);
        mc.setZoom(10);
    }
    protected void onDestroy() {
        super.onDestroy();
        mMapView.removeCallbacks(Timer_Tick);
        Field fConverterInView = MapView.class.getDeclaredField("mConverter");
        fConverterInView.setAccessible(true);
        fConverterInView.set(mMapView, null);

        Field fControllerInView = MapView.class.getDeclaredField("mController");
        fControllerInView.setAccessible(true);
        fControllerInView.set(mMapView, null);

        Field fZoomHelperInView = MapView.class.getDeclaredField("mZoomHelper");
        fZoomHelperInView.setAccessible(true);
        fZoomHelperInView.set(mMapView, null);

        Field fMapInView = MapView.class.getDeclaredField("mMap");
        AccessibleObject.setAccessible(new AccessibleObject[] { fMapInView }, true);
        fMapInView.set(mMapView, null);

        Field mConfigField = MapActivity.class.getDeclaredField("mConfig");
        mConfigField.setAccessible(true);

        Object mConfig = mConfigField.get(this);
        if (null != mConfig) {
            Field mConfigContextField = mConfig.getClass().getDeclaredField("context");
            mConfigContextField.setAccessible(true);

            mConfigContextField.set(mConfig, null);
            mConfigField.set(this, null);
        }
    }
}

编辑:: 我相信mNetworkHandler和我的Activity的上下文让彼此保持活力。从MAT开始,这是泄漏到我的上下文的内存跟踪:

------------------------------------------------------------------------------------------------------------------------
class android_maps_conflict_avoidance.com.google.googlenav.StartupHelper @ 0x43a4aef0 System Class                      
'- startupCallbacksForBgThread java.util.Vector @ 0x43a4b028                                                            
   '- elementData java.lang.Object[10] @ 0x43854810                                                                     
      |- [2] android_maps_conflict_avoidance.com.google.googlenav.map.MapService$1 @ 0x434e8d80                         
      |  '- this$0 android_maps_conflict_avoidance.com.google.googlenav.map.MapService @ 0x43abc930                     
      |     '- flashService android_maps_conflict_avoidance.com.google.googlenav.map.MapFlashService @ 0x43abcb00       
      |        '- store android_maps_conflict_avoidance.com.google.common.io.android.AndroidPersistentStore @ 0x4356baf8
      |           '- context com.my.package.MyMapActivity @ 0x436da020                                        
------------------------------------------------------------------------------------------------------------------------

以下是MapActivity对象的内存泄漏:

------------------------------------------------------------------------------------------------------------------------
class android_maps_conflict_avoidance.com.google.googlenav.StartupHelper @ 0x43a4aef0 System Class                      
'- startupCallbacksForBgThread java.util.Vector @ 0x43a4b028                                                            
   '- elementData java.lang.Object[10] @ 0x43854810                                                                     
      |- [3] android_maps_conflict_avoidance.com.google.googlenav.map.MapService$1 @ 0x43837938                         
      |  '- this$0 android_maps_conflict_avoidance.com.google.googlenav.map.MapService @ 0x43201cf8                     
      |     '- flashService android_maps_conflict_avoidance.com.google.googlenav.map.MapFlashService @ 0x43238470       
      |        '- store android_maps_conflict_avoidance.com.google.common.io.android.AndroidPersistentStore @ 0x435e6730
      |           '- context com.my.package.MyMapActivity @ 0x43822250                                        
      |              '- mNetworkHandler com.google.android.maps.MapActivity$1 @ 0x43822488                              
------------------------------------------------------------------------------------------------------------------------

AndroidPersistentStore持有对Activity的上下文的引用,我的上下文在MapActivity中持有对mNetworkHandler的引用。我不知道如何释放这两个对象。

0 个答案:

没有答案