Should I worry about memory leaks and using WeakReference with Volley in Android -
Should I worry about memory leaks and using WeakReference with Volley in Android -
after reading this article, started thinking memory leaks volley. usually, listeners implemented volley have either implicit or explicit reference outer class (the activity). example:
jsonobjectrequest jsonobjreq = new jsonobjectrequest(request.method.get, url, null, new response.listener<jsonobject>() { @override public void onresponse(jsonobject response) { updatelayout(); } } in case there implicit reference... or may want create custom jsonobjectrequest internalize response handling, , need pass in reference calling activity in constructor.
now lets start web request, before response comes back, navigate away activity started request. understand jsonobjectrequest object maintain reference activity , prevent beingness garbage collected. -am understanding correctly, legitimate fear? -does volley library automatically deal this? -if creating custom jsonobjectrequest , passing in "this" (reference activity), need create weakreference activity?
based on looking @ volley code, calling cancel doesn't avoid memory leak because reference never gets cleared , reference isn't weak. calling cancel avoids volley delivering response listener.
my solution problem have cloning , modifying library myself.
one of solutions can create base of operations errorlistener reference within of base of operations request.java weak reference. , same can done listener within of jsonrequest.java.
the other solution can manually clear reference upon cancel beingness called. within of cancel(), set merrorlistener , mlistener null. solution though, you'll have remove final modifier field declaration otherwise wouldn't allowed set reference null.
hope helps.
android android-volley
Comments
Post a Comment