Here are some newbie memory management observations to which I would like hear an experienced opinion.
It seems that setting android:backgound="@drawable/xyz" in a xml layout causes memory loss in my app. The respective activities keep stacking up until I get an OOM error. This is especially true if I rotate the device orientation.
However, if I load the same resource with setBackgoundResource(), and then clear the callback and set the background reference to null, there is no leak whatsoever.
that is, first in onCreate()
mMainLayout.setBackgroundResource(R.drawable.background_general_android);
and then in onDestroy()
mMainLayout.getBackground().setCallback(null);
mMainLayout.setBackgroundDrawable(null);
Is this roughly correct, or am I missing something essential?
This would only happen if you keep a copy of the drawables in a static cache for instance. You might also be leaking your activities and setting the drawables to null simply hides the problem for a little longer. You should use a tool like MAT to inspect the content of your heap and figure out what's going on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With