I used MAT tool in Eclipse to investigate a memory leak issue and found that, occasionally, a CookieSyncManager thread instance leaks my activity. The path from my activity to GC root is as following:
com.mycompany.myapp.MyActivity
--> mContext com.android.internal.policy.impl.PhoneFallbackEventHandler
--> mFallbackEventHandler android.view.ViewRoot
--> target android.os.Message
--> <java local> java.lang.Thread CookieSyncManager Thread
MyActivity called CookieSyncManager.createInstance(this.getApplicationContext()); in onCreate(), but it doesn't use any webview. It only contains some animations. I don't understand why it is leaked by CookieSyncManager. Can someone help?
Thanks.
You could call CookieSyncManager.createInstance(this.getApplicationContext()); using a context wrapper that only holds a weakreference to the actual context.
In such a case you would delegate all calls to the weakly referenced context which will automatically be cleared when there a no other strong references to it. Just make sure you perform a null check before accessing the actual context like this.
Context realContext = mMyWeakReference.get();
if (realContext != null) {
// delegate call to real context
realContext.delegateToWhateverFunctionWasCalled();
}
storing it in a local variable is important and could prevent an npe when the reference is cleared while your code runs.
This kinda looks like a potential framework issue, though if the framework clears this reference a bit later it should also be fine. And in that case it may not be a real issue at all, just a false positive.
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