Where Weak and Soft references are used in Java EE programming

363 Views Asked by At

I am Java EE developer but I don't know where in day to day programming one might use Weak or Soft references.

2

There are 2 best solutions below

0
On

You don't not normally use them in day to day programming (at least i don't and i think most of us don't) but they can be very useful!

practical scenarios for me would be:

WeakReference for debugging purpose (e.g. keep track of open database connections).

SoftReference for a quick and dirty cache (e.g. caching large java POJOs that are "expensive" to create and should not be cleaned up immediately).

PhantomReference is really impracticable in my opinion.

0
On

There are many good references (nice pun!) and I suggest you Google Bob Lee, "the ghost in the virtual machine."

In a nutshell, a SoftReference is occasionally useful as a quick and dirty cache, but they aren,t that useful. WeakRefences are not for caching, but your listener lists should definitely use them so that unneeded listeners can be garbage collected.