1

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.

Brian McCormick
  • 449
  • 4
  • 5
  • 2
    I don't think Java can do this. The closest thing I can think of is to keep a minimum set in memory, put a second set in SoftReference, and then the rest of the blocks in WeakReference. That'll give you some semblance of a priority system, though it'll be pretty imperfect. – markspace Apr 19 '20 at 16:08
  • I find this a wonderful question that I have faced before, unfortunately I do not have an answer for it. – Eugene Apr 22 '20 at 13:52

1 Answers1

0

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.

CodeScale
  • 3,046
  • 1
  • 12
  • 20