Java SoftReference priority

133 Views Asked by At

I have an app that has several very large blocks of data which take a long time to compute. I hold each of these as a SoftReference because I can recompute them but it takes a long time. So as long I have enough memory I want to reuse what I have already calculated. This is precisely what the Java SoftReference is for.

However, what I really want to do is to rank these blocks of data because some are more important and take much longer to compute than others so I want to have the garbage collector collect those last.

I don't want a solution where I write these blocks off to disk. I know how to do that. I just want to be able to set the order/priority of garbage collection on my soft references.

1

There are 1 best solutions below

0
CodeScale On

No guarantees are placed upon the time when a soft reference gets cleared or the order in which a set of such references to different objects get cleared.

As a rule, JVM implementations choose between cleaning of either recently-created or recently-used references.