WeakReference
and SoftReference
are used when you want to keep something around in case you need it again - but you might not need it and if you do need it you can recreate it.
For example if you have a Cache of information you've fetched from a website, you don't want to constantly re-fetch it but if you need memory you can always drop something you haven't used for a while and get it back again if you do need it.
SoftReferences
in particular are useful for this sort of caching as it tells the GarbageCollector not to get rid of the objects unless it really needs to free up the memory.
WeakReference
on the other hand the GC can clean up as soon as it likes.
I've used them before combined with a factory pattern. Keep a SoftReference
to objects when you create them in the factory. If they are asked for again then return the already-created object. If they don't exist or have been garbage collected then create them, return them, and keep a SoftReference
inside the factory.