Anonymous Listener of volley request causing memory leak

后端 未结 4 1864
别跟我提以往
别跟我提以往 2021-01-05 07:14

I am using volley library for making web-services call. I made a general class for making all web services call and making service call from there and made anonymous listene

4条回答
  •  感情败类
    2021-01-05 07:36

    Basically the anonymous approach is terrible in Android or in any ClientSideSystem where you don't have massive memory. What is happening is, you have passed Context as parameter in method and anonymous holds a reference of it. The real mess comes now in the scene when the thread inside which makes network call could not finish it's job and before that the calling activity for some reason either destroys or recycles in that case GC is not able to collect the activity as wokerThread might still be holding reference onto it. Please go through this for detail description.

    The solution could be either static inner classes or independent classes, in both cases use WeakReference to hold resources and do a null check before using them.

    Advantage of WeakReference is it will allow GC to collect the object if no-one else if holding reference onto it.

提交回复
热议问题