Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does Android leak memory due to static Drawable if it's callback is reset?
    primarykey
    data
    text
    <p>I was just following this article on how to avoid memory leaks : <a href="http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html">android developer blog</a> Following is the code snippet used :</p> <pre><code>private static Drawable sBackground; @Override protected void onCreate(Bundle state) { super.onCreate(state); TextView label = new TextView(this); label.setText("Leaks are bad"); if (sBackground == null) { sBackground = getDrawable(R.drawable.large_bitmap); } label.setBackgroundDrawable(sBackground); setContentView(label); } </code></pre> <p>It is said that the drawable has a callback reference to textview (and indirectly to the activity); which will be preserved on rotation - and hence memory leak. </p> <p><strong>My query</strong> is that won't the drawable's callback be reset on rotation - it would get hold of the new textview (which will hold the new context).. hence allowing the previous instances of textview/context to be GC'ed.</p> <p><strong>EDIT</strong> : The answers I get are on how to "solve" the issue - I am not looking for that ! Please re-read the query. I am adding more details. When activity is launched, the references are :</p> <blockquote> <p>Drawable1 -> TextView1 -> Activity1</p> </blockquote> <p>When rotated, Activity1 and TextView1 are destroyed but not Drawable1</p> <blockquote> <p>Drawable1 -> TextView2 -> Activity2</p> </blockquote> <p>This means, <strong>Activity1 and TextView1 are free to be GC'ed</strong> - as no other object is having a reference to them. So what is leaking ?</p> <p>Am I wrong in this understanding ? Or is it that the Drawable can have multiple views as callbacks ? (Looking at the source code, I dont see a list of callbacks on Drawable). </p>
    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.
    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