Why does my code leak when switching activities?
<p>I have written a mapping application which can either use Google Maps or Open Street Map
as a tile provider. The Google and OSM maps are displayed in separate activities. After the
splash screen a select mode activity is entered. From this screen the user can either 
choose the Google or OSM activty via a button.</p>

<p>I'd like to be able to switch between Google and OSM via a button in each mapping activity. When I code the click handler for each of the mapping activities, I have in each:</p>

<pre><code>i = new Intent("com.me.otheractivity");
finish();
startActivity(i);
</code></pre>

<p>I have no service connection, or inner classes for overlays etc. anywhere in the code.
When I traverse from Select->Google-> Select(via back button) -> OSM , all is fine in terms
of allocated heap.</p>

<p>If I go directly from one mapping activity to the other repeatedly, then the allocated heap
grows and eventually crashes at 16M. Obviously it must be leaking somewhere via this route.</p>

<p>I'm logging every onCreate, Start, Resume, Pause, Stop and Destroy in all 3 activities.
If I use the back button route I have the log:</p>

<p><strong>Select Activity -> Google Activity -> OSM Activity (direct via button in Google Activity)</strong></p>

<pre><code>Select Activity Pause 
Google Activity Create
Google Activity Start 
Google Activity Resume
Select Activity Stop 
Google Activity Pause 
Open SM Activity Create 
Open SM Activity Start 
Open SM Activity Resume 
Google Activity Stop 
Google Activity Destroy
</code></pre>

<p>Going  via back button, I get </p>

<p><strong>Select Activity -> Google Activity -> Select Activity -> OSM (via back button)</strong></p>

<pre><code>Select Activity Pause 
Google Activity Create 
Google Activity Start
Google Activity Resume 
Select Activity Stop
Google Activity Pause 
Select Activity Start 
Select Activity Resume 
Google Activity Stop 
Google Activity Destroy
Select Activity Pause
Open SM Activity Create
Open SM Activity Start
Open SM Activity Resume
Select Activity Stop 
</code></pre>

<p>In the first example the Google Activity is not stopped or destroyed until after the OSM 
one is created, started and resumed. Is this significant regarding the leak?</p>

<p>I set to null all timers, handlers and overlays in the onPauses. (Combining the two views in one activity is not really an option owing to the differences between the Google maps.jar and osmdroid.jar)</p>

<p>Is anything wrong with the code in my click handlers?</p>

<p>All suggestions will be gratefully received.</p>

<p><strong>EDIT 26th Feb</strong></p>

<p>Further to my original post - the salient point for me is :</p>

<p><em><strong>Why is it necessary for the onDestroy in one activity to run before the onResume in the second activity for the memory usage to stop growing?</em></strong></p>

<p>If the onResume in activity B runs before the onDestroy in activity A, then I see that the number of activities on the history stack (as reported by adb shell dumpsys meminfo) increase by one each time. No amount of forced GC either in the code or via DDMS will take them off the stack.</p>

<p>I've since modified my code so that the clickhandler just calls finish(). In the onDestroy I call the startActivity(). This briefly returns the screen  to the select mode activity before running the other activity. Under these circumstances obviously the onDestroy() in A runs before the onResume() in B and neither the history stack or heap usage grow.</p>

<p>I just don't get it.</p>
