4

I've recently been playing around with soft, weak and phantom reference types in Java and have been wondering if there's any uses out there for them that I haven't come across. I've used them in the past for various things and they've always fallen under the category of the following:

  • Using phantom references in place of finalizers - I prefer this since there's 0 chance of a dead object being resurrected
  • Using weak references in a hashmap to hold an object=>value mapping, where the mapping should only be in place if an object exists elsewhere (useful when needing to add extra information to an object in a library for instance whose source can't be modified)
  • Using soft references for caching (works much better than weak references because the JVM keeps them around for much longer and only lets go if it feels it needs to.)

However, there's only 3 uses there and I daresay there's lots more things that they might be useful for which I've never come across. All suggestions welcome!

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
  • I originally intended this to be a community wiki but couldn't find the option to select, does it need to be done by someone with more permissions or am I just being thick? – Michael Berry Jan 21 '11 at 01:36
  • I think it's only possible for the answers, s. http://stackoverflow.com/privileges/community-wiki – maaartinus Jan 21 '11 at 01:53
  • I think that's pretty much it according to my readings. Although I never bought the idea of using soft ref for caching. I would like better control of cache policies, not in the hand of GC. – irreputable Jan 21 '11 at 02:13
  • I agree that soft refs aren't a universal do-it-all cache, though I quite like the fact that they're in the hands of GC (when it's implemented properly, of course.) There's stuff that I want to stick around if preferable but if running low on memory I don't mind it being cleared up, and for that mindset soft refs fit the bill perfectly! – Michael Berry Jan 21 '11 at 02:15
  • Yes, except for you having no control over the order in which they get cleared. Some cached object may be large and easy to recompute, other may be small and hard to recompute, you'd surely prefer to keep the latter, right? – maaartinus Jan 21 '11 at 03:27
  • @maaartinus Sure thing - in that case you wouldn't use them. That's why I explicitly said "soft refs aren't a universal do-it-all cache" ;) – Michael Berry May 31 '12 at 15:51

1 Answers1

1

Two strange ideas:

  • You may use a soft reference for finding out you're low on memory and manually freeing some caches which themselves can't use soft references.
  • You may use a weak reference to find out when the GC runs, which may be useful in case you're experiencing strange program pauses which may or may not be related to the GC.

IMHO, in some (rare) cases weak references may be better for caching, e.g., you may weakly refer to values, which are improbable to be needed again once they get removed from the structures using them (i.e., they get strongly unreachable). Moreover, there's a serious bug in the JVM concerning soft references, which may force you to do so.

maaartinus
  • 44,714
  • 32
  • 161
  • 320