WeakReference does not prevent the collection of the object, therefore, if the object belongs to a young space and is accessible only by weak links, it will be assembled.
SoftReferences (in JSM HotSpot) act either as weak or strong links, depending on the latest timestamps of access (timestamp of the last call to get() on the link). The "expiration time" of the sotf link is calculated as the free memory size multiplied by the coefficient configured via -XX:SoftRefLRUPolicyMSPerMB=T (for example, less free memory, short expiration of soft links).
Usually, using JVM links for caching is a bad idea: - it moves temporary objects into the old space, violating the weak hypothesis of generations - links pose a special threat through GC, which increases the insignificant time of GC
If you need a cache with an eviction strategy, it is better to implement it explicitly and take into account the effect of the occupied memory on the stored data in the eviction policy.
Alexey Ragozin
source share