On Jul 22, 4:09 pm, Joseph Earl <joseph.w.e...@gmail.com> wrote:
> I am confused somewhat about the issue myself now. Hopefully someone
> else it will clear it up once and for all.
>
A great explanation is here: http://jnb.ociweb.com/jnb/archive/jnbJune2000.html

Use weak references when you are creating a reference to an object
somewhere (usually a collection or another thread) but you don't want
that reference to prevent it from being GC'd, or "deleted".  The
reason they use weak references in the example you looked at is that
they are creating a separate thread from the main app thread.

If the main thread doesn't need those objects any more, they would
normally be deleted.  But if you've got a background thread going,
with references to those objects, then they won't be deleted until the
background thread is finished.  Since, in the given example's case,
the background threads are "useless" without the main thread, we don't
want the background thread to prevent those objects from being
deleted, so they are created with weak references.

NOTE: If you never used weak references, just regular "strong"
references, in the scenario with a main app thread and a background
thread, then everything would still work.  When the main thread ends,
but the background thread is still going, the background thread's
objects aren't deleted yet.  But when the background thread dies, then
they are deleted.  What you gain by using weak references is just a
quicker way to delete those objects on the background thread when the
main thread ends. I would guess that, in a typical "background
download" scenario, by using weak references instead of regular
"strong" references all you're doing is keeping the memory from being
reclaimed for at most 30 seconds.  The amount of time depends on
what's going on in the background, and the amount of memory depends on
your app.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to