android - Image adapter leaking memory -
i have simple listactivity shows images , inizialize okhttpclient picasso builder in constructor of imageadapter class:
picassoclient = new okhttpclient(); picassoclient.interceptors().add(new interceptor() { @override public response intercept(chain chain) throws ioexception { request newrequest = chain .request() .newbuilder() .addheader("cookie","xyz") .build(); return chain.proceed(newrequest); } }); new picasso.builder(context).downloader(new okhttpdownloader(picassoclient)).build(); then in getview() use picasso load images in imageview:
picasso.with(context).load(xyzurl).fit().centercrop().into(vimage); it works well, on device's rotation see heap size grows, , remains stable. drops. leaking memory or there wrong in code?
edit: inserted code after picasso's call in getview()
if (buildconfig.debug) { log.i("heap size", string.valueof((runtime.getruntime().totalmemory() / 1024) - (runtime.getruntime().freememory() / 1024))); } and found heap size's growth happens in getview() after loading bitmap imageview. wrong?
edit 2: tried set static imageadapter, nothing changes
edit 3: tried recyclerview instead of listview, same behavior: heap size grows continuously while scrolling image list stepping 30-40 bytes @ every onbindviewholder(). after device's rotation heap size grows stepping 2-3 mbytes. drops.
why heap size continuously grows , why leaking cache or cached bitmaps after device's rotation?
update: tried adapter without code in constructor (that without new okhttpclient , new picasso.builder), works , the heap size drops remaining stable. then, correct way initialize client cookies headers management?
upshot: created picassoinstance class, creates unique static picasso singleton , set picasso library's singleton. set in adapter constructor
picassoinstance.setpicassosingleton(context);
it works well, , correct way hope.
public class picassoinstance { private static picasso mypicassoinstance = null; public static void setpicassosingleton(context context) { if (mypicassoinstance == null) { mypicassoinstance = createmypicassoinstance(context); picasso.setsingletoninstance(mypicassoinstance); if (buildconfig.debug) { log.i("picasso instance", "created"); } } } private static picasso createmypicassoinstance(context context) { okhttpclient myokhttpclient = new okhttpclient(); myokhttpclient.interceptors().add(new interceptor() { @override public response intercept(chain chain) throws ioexception { request newrequest = chain.request().newbuilder() .addheader("cookie", "xyz").build(); if (buildconfig.debug) { log.i("on intercept", "cookie added"); } return chain.proceed(newrequest); } }); return new picasso.builder(context).downloader( new okhttpdownloader(myokhttpclient)).build(); } }
the picasso instance being returned picassobuilder.build() should singleton, , when need use picasso throughout app should accessing singleton, instead of picasso.with... should accessing
yourclass.getmypicassosingleton().with... otherwise you're keeping separate caches, etc picasso instances
edit: noted below, can call
picasso.setsingletoninstance(mypicasso); right invoke build method above, solve problem without holding onto singleton yourself. cleaner solution
Comments
Post a Comment