I partly got the roles of SoftReference and WeakReference mixed up. For the cache you would more likely use SoftReference, and it's the one that is preferentially not deleted by GC. WeakReferences don't slow deletion at all (well, hardly any -- there's the whole queue thing, but that's for the graduate level class). So a WeakReference would be used where you want to have a reference to an object if it exists, but not prevent its deletion.
Let's say that you have a Department object that tracks all of the members (Employees) of a department in BigBlueCompany. But you also have, of course, a Payroll object that knows all of the employees for the entire company, and when someone gets cut from the Payroll that's it -- they're kaput, gone, vanished. If a WeakReference to the Employee is used in the Department object, as soon as the Employee ref is stomped in the Payroll object (and a GC cycle done) the Employee is suitably disappeared, without having to remove it from the Department. (In real life this is especially handy if you're keeping refs into another structure where the manager of that structure does't know about your refs and the need to remove them. With normal refs vs WeakReferences this would lead to a storage leak, since objects the manager thought were deleted were still referenced by you and were not getting cleaned up. And you'd not know when an object was considered deleted by the structure's manager.) On Jul 22, 1:21 pm, DanH <[email protected]> wrote: > The real Java doc is a bit > better:http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/l... > > A WeakReference is an object that contains within it a reference to > another object. The reference is treated specially by the garbage > collector in that its existence doesn't prevent the object from being > deleted (though any other "real" reference will). But, if the object > IS deleted, the garbage collector nulls the pointer in the > WeakReference. > > You can examine the actual object reference using the ref() method. > > You might, eg, create a cache (say, of URLs resolved to IP addresses > by network name lookup) by using a hashtable of String ->WeakReference. When > you want to reference something you look it up > > in the hashtable and, if not found, create it. Later, when you look > again and find the hashtable entry you check ref() and treat it as > "not found" if the ref() is null, otherwise use the ref() value to > reference the object. Garbage collection will preferentially not > delete objects referenced from WeakReferences, but may eventually > delete them, after several GC cycles of being otherwise unreferenced. > > On Jul 22, 12:48 pm, GodsMoon <[email protected]> wrote: > > > Google just posted a new blog post > > onhttp://android-developers.blogspot.com/2010/07/multithreading-for-per.... > > I understand the AsyncTask and I'm even using one in a list with > > images already. > > > But I don't understand what a WeakReference is. I gather is is a > > garbage collector directive, but I thought I didn't need to manage > > garbage collection on Android. > > >http://developer.android.com/reference/java/lang/ref/WeakReference.html > > isn't as helpful as I was hoping it would be. -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

