In our project, we detected memory leaks due to network callbacks. The network request is launched from the fragment, and the response is returned via the fragment callback. The problem is that when the user leaves the fragment, it is not garbage collected, since the callback is bound to it. Therefore, there is a memory leak.
My suggested solution was to collapse the callback link to the onStop fragment. In this way, the GC can take care of this.
Another solution suggested by my staff member is to use for WeakReference for callbacks. The problem with this is the callback, which often collects garbage, so that we donโt even get a response from the callbacks (some time when the user is waiting for an answer). The problem is that the โWeak Linkโ can be compiled using the GC at any time.
I assume that in this case using WeakReference is not a good idea.
What do you guys think?
source share