Here's a piece of simplified code:
static Activity longLivedField;
onCreate(...) {
longLivedField = this; // the only write to this field
}
I've seen people claiming this as a context leak, and create fixes for it. The typical fix is to nullify the field at appropriate places. For example, in onPause()
:
onPause() {
longLivedField = null;
}
Yes, it is a memory leak if you don't nullify the field in onPause(). You almost certainly never want to retain a static reference to any Activity. What is it that you are trying to achieve?
The Android developer web site contains a handy page describing how to avoid memory leaks like this one:
Avoiding Memory Leaks
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