Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: OT SoftRefHashtable uses SoftReferences



Nicholas R. Rinard wrote:
I recently implemented a Hashtable which uses SoftReferences instead of regular references. I thought this code might be useful to someone else, since it has helped me be a little more gentle with my memory footprint. I also though I might slyly benefit if someone notices a bug that I overlooked. This code is simple, almost trivial, so it can be public domain, but it is also useful, so especially if you notice a bug please let me know.

I do have a couple of comments about your implementation.

First, the get method:


    public Object get( Object key ) {
        SoftReference sr = (SoftReference) super.get( key );
        if( sr == null ) {
            return null;
        } else {
            Object ret = sr.get();

            if( ret == null ) {
                remove( key );
            }
            return ret;
        }
    }

I think calling remove here could cause you problems with ConcurrentModificationExceptions if you use an iterator.  The last time I checked, the iterators just call get, and since the iterators  are fail fast, they will fail if calling get changes the  underlying collection.  I believe this is why WeakHashMap only purges gc'd members when a mutator method is called.

    public int size() {
        int ret = 0;
        Enumeration e = keys();
        while( e.hasMoreElements() ) {
            Object o = get( e.nextElement() );
            if( o != null )
                ret ++;
        }
        return ret;
    }

So it returns the number non-null (i.e. non gc'd) elements.  I assume this is because Hashtable is defined to not support null values.  This seems like it could cause you some headaches, however, since other methods, like keys and keySet will return a different number of elements.  Also, your elements method:

    public Enumeration elements() {
        Vector v = new Vector();

        Enumeration e = super.elements();
        while( e.hasMoreElements() ) {
            Object o = ((SoftReference)e.nextElement()).get();
            v.addElement( o );
        }

        return v.elements();
    }


will return all the elements, even if they are null.  This is unexepected, given the implementation of the size method. 

Another thing I noticed is that you didn't override entrySet and some other methods from the Map interface that Hashtable now implements.  Perhaps you only cared about the original Hashtable interface.

In the end, it might be easier to have size just return the actual size, and let your implementation return null values.

Rob
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden

References: 
 >OT SoftRefHashtable uses SoftReferences (From: "Nicholas R. Rinard" <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.