Issue
I have read that using asynctask as an inner class of an activity will cause memory leaks because it won’t let the activity get garbage collected and also due to implicit referencing as well. Will the same memory leaks occur if we used different approaches like:-
- Using AsyncTask in a seperate class ?
- Or if we used AsyncTask with a headless fragment (using its
setRetainInstance(true)
method to avoid its destruction).
I have implemented the headless fragment approach and it works great since the asynctask object is created only once and its controlled by the headless fragment but i can’t figure out if the memory leak of the activity problem is gone or if its still there.
Solution
First of all you could use a library as LeakCanary or learn how to use the android profiler to find leaks. Consider that not every expert agrees that small leaks have to be fixed, some of them are really small and won't cause big troubles.
the headless fragment approach should be good. To reply to your first question, consider that you can make static the inner class as indicated here where is explained also how to make a weak reference that would be easily garbage collected
Answered By - trocchietto
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.