Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does heap memory increase when re-launching an activity?
    primarykey
    data
    text
    <p>This question concerns memory in Android.</p> <p><strong>My method:</strong></p> <p>I have two activites, A and B. From A, I launch B like this:</p> <pre><code>Intent i = new Intent(A.this, B.class); startActivity(i); </code></pre> <p>On button click in B, I do this:</p> <pre><code>B.this.finish(); </code></pre> <ul> <li>In B, I override the onDestroy method and set all references to null. </li> <li>I do not allocate new memory in the onResume method of A. </li> <li>I am not leaking a context. </li> <li>I am not using multiple threads. </li> <li>I am not using services.</li> <li>All variables in B are private class variables, and all of them are set to null in the onDestroy of B. </li> <li>Also, ImageViews in B have their background set null in the onDestroy of B. </li> <li>I am certain that B gets destroyed.</li> </ul> <p><strong>The result:</strong></p> <p>When I am in Activity A, heap memory is at 7.44 MB. Then when I start B and call finish on B(and thus returning to A), heap is increased by 0.16 MB. Repeating this process again, heap is increased by 0.08 MB every time.</p> <ul> <li>I'm not looking at the heap limit, I'm looking at the allocated heap.</li> <li>I'm calling System.gc() at the end of the onDestroy method of B.</li> </ul> <p><strong>Additional info:</strong></p> <p>-I have used MAT to analyse memory allocations and try to find this leak. Something strange is that Activity B seems to have 5 instances. As it so happens, I was repeating the startActivity/finish process 5 times. The bottom entry is the Activity, the others are listeners in the activity:</p> <p><img src="https://i.stack.imgur.com/rQVzF.png" alt="enter image description here"></p> <p>And this is screenshot of the dominator tree. I can't find anything unusual or suspect.</p> <p><img src="https://i.stack.imgur.com/27eCf.png" alt="Dominator Tree"></p> <p>-I have watched both google IO videos on memory usage(and leaks).</p> <p><strong>Question:</strong></p> <p>Is it possible that this 0.08 MB of heap will always be allocated(and not collectable by the GC) no matter what I do? If not, any idea of what might be causing this?</p> <p><strong>Update:</strong></p> <ol> <li><p>I tried to launch activity B without setting a content view in B. This means that B is a completely empty activity. The result was that the heap memory did NOT increase when I'm relaunching the activity several times. Note, however, that this is no solution. I must be able to set a content view.</p></li> <li><p>scorpiodawg: I tried running my app on an emulator, and the heap still grows. Good try though.</p></li> <li><p>ntc: I changed all occurences of "this" to "getApplicationContext()" where it was possible. I could not call setContentView(getApplicationContext()); because setContentView wants a reference to a layout file, not a context. What I did instead was to create an empty layout file and call setContentView(emptylayout); in the onDestroy method of Activity B. That did not help.</p></li> <li><p>I tried to remove all the code so that only setContentView(mylayout) gets called. Problem persisted. Then I removed all the gui elements in the layout XML file. Problem persisted. The only thing that was left was the container views, a couple of nested linear, relative and scrolllayouts. I tried to remove setting the "android:scrollbarDefaultDelayBeforeFade" attribute in the scrollbar. The result was great, the memory leak had vanished. Then I put back all the code I previously removed but didn't set the "android:scrollbarDefaultDelayBeforeFade" attribute and the memory leak was back. How strange is that?</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload