0

I am new to this SoftReference and WeakReference stuff. I am making a generic ViewHolder pattern which keeps all views in Hashtable keyed by View's Id. I decided to use SoftReference for Hashtable and also SoftReference for value of item inside Hashtable.This is my ViewHolder class

public class ListItemTagHolder {
    private int index;
    private SoftReference<java.util.Hashtable<Integer,SoftReference<View>>> views=
            new SoftReference<java.util.Hashtable<Integer,SoftReference<View>>>(
                    new java.util.Hashtable<Integer,SoftReference<View>>());

    public ListItemTagHolder(int index){
        this.index=index;
    }

    public void destroy(){
        java.util.Hashtable<Integer,SoftReference<View>> vals=views.get();
        Enumeration<Integer> ks=vals.keys();
        while(ks.hasMoreElements()){
            vals.get(ks.nextElement()).clear();
        }
        vals.clear();
        views.get().clear();        
        views.clear();
        ks=null;
        vals=null;
    }

    public View getView(int id){
        return views.get().get(id).get();
    }

    public void addView(View v){
        views.get().put(v.getId(),new SoftReference<View>(v));
    }

    public int getIndex(){
        return index;
    }

    public void setIndex(int index){
        this.index=index;
    }
}

I want to know the following

  1. Is it correct usage of SoftReference ?

  2. What will happen if my activity is destroyed and I am unable to call destroy() method of ListItemTagHolder. Will the memory get cleaned up for Hashtable ?

  3. Will all the items in Hashtable be available to me everytime unless all reference to ListItemTagHolder are removed ?

source.rar
  • 8,002
  • 10
  • 50
  • 82
Rameez Usmani
  • 135
  • 1
  • 8
  • does it really pay off to make such generic ViewHolder stuff? i assume you know what VH is for and how often it's used? – pskink Jun 11 '14 at 06:12
  • You shouldn't be using anything other than Strong References for the ViewHolder pattern. The ViewHolder pattern is designed to speed up the population of views in an adapter. By using some kind of weak references for the View Holder just slows everything down as they are more expensive compared to Strong Refs. Secondly your concern about the hash table being destroyed shouldn't be about the application lifecycle, but the runtime itself, Dalvik for instance. References other than Strong are at the mercy of the runtime, so you can't rely on them being in memory. – nyarlathotep77 Jun 11 '14 at 06:38
  • @nyarlathotep77 it's not used for " population of views " but for accessing them, its a replacement for findViewById – pskink Jun 11 '14 at 06:53
  • Yes, and why do you use findViewById? To get a reference to a widget you want to do something with which generally is populating with your data :) – nyarlathotep77 Jun 11 '14 at 07:00
  • @pskink I know why ViewHolder is used. My app has around 10-12 different activities having ListViews. Each ListView has different type of data and layouts. That is why instead of creating 10-12 different ViewHolder or placing reference to all possible controls in one class I created this generic type of thing. Its helping me a lot. – Rameez Usmani Jun 11 '14 at 07:56
  • @nyarlathotep77 thanks for the details. I will change it to Strong Reference and will also read more about these things. Thanks everyone – Rameez Usmani Jun 11 '14 at 07:57
  • @RameezUsmani i mean don't use holder pattern at all, i didn't see any performance boost when using it, CursorAdapter doesn't use it and i wouldn't say it's slow – pskink Jun 11 '14 at 08:06

0 Answers0