RFR 9: 8138696 : java.lang.ref.Cleaner - an easy to use alternative to finalization
Peter Levart
peter.levart at gmail.com
Wed Nov 4 14:38:38 UTC 2015
On 11/03/2015 10:42 PM, Roger Riggs wrote:
>>>> I’m in two minds that it sounds sensible for this Cleaner API to
>>>> extend for soft/weak references whereas I am not certain of the use
>>>> cases. Anyone can share the known use cases that would be helpful.
>>>>
>>> Besides the cases already mentioned on the thread... Anyone?
> A developer at JavaOne wanted more control over the policy for
> clearing SoftReferences
> possibly taking into account the cost of creating the cache entry.
> A technique I proposed was to use a SoftReference, not for the cached
> resource itself
> but as a sentinel for the VM needing to free 'soft' memory.
>
> One possible use for a SoftCleaner is to implement a sensible cleaning
> strategy for
> a Cache. The CleaningFunction was would be triggered when freeing
> SoftReferences
> was needed by VM GC policy and could be used as a hook to use its own
> algorithm
> to free some of its cached values.
...and register another Soft CleaningFunction at the same time...
Clever! So you get periodic call-backs from GC when GC thinks it's time
to do some soft cleaning, and implement the strategy of cleaning the
data structure yourself, like:
void doPeriodicCleaningWhenGcThinksItsTime(Cleaner cleaner, Runnable
strategy) {
cleaner.softCleanable(new Object(), () -> {
strategy.run();
doPeriodicCleaningWhenGcThinksItsTime(cleaner, strategy);
});
}
Regards, Peter
More information about the core-libs-dev
mailing list