title: Android MapActivity leak in my Activity's Context related to mNetworkHandler
body: <p>I'm having difficulties tracking down the cause of a leak from my use of MapActivity. I have been using the Eclipse Memory Analysis tool for the past few months to track down various memory leaks within our application, but I can't seem to find a how-to that explains the usage of MapActivity and how to clean up the resources properly.</p>
  
  <p>From MAT, the memory leak seems to be coming from com.google.android.maps.MapActivity's mNetworkHandler keeping My Activity's context alive, and vice versa. On this particular screen, the leak surfaces with each new latitude/longitude that is fed into the MapActivity. If I continually go to this screen with the same coordinates, the leak does not grow.</p>
  
  <p>Below you'll find the (condensed) code that I'm working with. </p>
  
  <pre><code>public class MyMapActivity extends MapActivity
  {
      MapView mMapView;
      List&lt;Overlay&gt; 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);
          }
      }
  }
  </code></pre>
  
  <p>edit::
  I believe the mNetworkHandler and my Activity's context are keeping each other alive. From MAT, this is the memory trace to the leak to my context:</p>
  
  <pre><code>------------------------------------------------------------------------------------------------------------------------
  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                                        
  ------------------------------------------------------------------------------------------------------------------------
  </code></pre>
  
  <p>Here is the memory leak from the MapActivity object:</p>
  
  <pre><code>------------------------------------------------------------------------------------------------------------------------
  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                              
  ------------------------------------------------------------------------------------------------------------------------
  </code></pre>
  
  <p>AndroidPersistentStore is holding a reference to my Activity's context, and my context is holding a reference to the mNetworkHandler within MapActivity. I don't see how to release these 2 objects.</p>