Passing a reference to an Activity in Asynctask defined as a separate Class , Will it lead to memory Leak if Activity is killed by User (Please don't consider screen rotation Scenarios im aware of that)
Asked
Active
Viewed 106 times
1
-
what are you trying to do with the activity reference? – tyczj Jul 06 '15 at 14:05
-
Im using it to initialize an interface to send callback back to activity – user1530779 Jul 06 '15 at 15:55
1 Answers
1
I've always read that in this scenario you should use a WeakReference
to the Activity
, so that the Activity
can be properly garbage collected iff there are no other hard references to it. This answer explains it much better than I could.

Community
- 1
- 1

TBridges42
- 1,849
- 1
- 19
- 29
-
I understand Weak References but Im doubtful as to how early that weak reference gets garbage collected , they have a mind of their own – user1530779 Jul 07 '15 at 05:57
-
Rather than that why not create a new method that onCancel(Asynctask) in which we can pass the asynctask reference and cancel it and then set the interface reference to null – user1530779 Jul 07 '15 at 06:44
-
You never have precise control over when the garbage collector does its job, but as soon as an object is only reachable by weak references, it is eligible to be collected. In the case study I read on garbage collection, objects with only weak references were collected after all objects with no references were collected, at a rate proportional to the memory pressure on the system. – TBridges42 Jul 07 '15 at 12:41