I just read the article 'Avoiding memory leaks' in android blog
written by Romain Guy.
http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html
According to the article, the below code has memory leak when rotating
because the static drawable has a reference to Activity.
----------------------------------------------------------------------------------------
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);
}
----------------------------------------------------------------------------------------
I understand sBackground has a reference to TextView, and the TextView
has a reference to Activity.
I know when a device is rotated, Activity is destroyed and created
again.
but I think when a new Activity is created, sBackground setCallback a
new TextView and the TextView has reference to the new Activity.
Then the old TextView and the old Activity is not refered by
sBackground so they can be garbage-collected.
which means this code has no memory-leaks.
Please correct me. Any help is appreciated.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
To unsubscribe, reply using "remove me" as the subject.