yf0994 / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Feature request: RemovalNotification able to guarantee non-null key/value #1138

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I'd like to write a cache that wraps a weak-valued Cache and may rescue an 
evicted entry when evicted. It would rescue it by storing a strong reference to 
the key/value in a secondary Map.

I think this is not possible today. Have you considered allowing the user to 
request that in your ReferenceQueue callback you store off a strong ref to the 
object so that RemovalNotification never has a null? Your map would still 
behave as advertised, the entry would be evicted, the only side effect is that 
the once-weakly/softly reachable object is now strongly reachable again when 
the user's RemovalListener is invoked.

Original issue reported on code.google.com by brianfromoregon on 6 Sep 2012 at 9:28

GoogleCodeExporter commented 9 years ago
I don't think this is an even possibly doable thing.

Once a reference gets into a ReferenceQueue, there are no more references to 
the referenced object in the entire VM, by definition.  If there's a strong 
reference anywhere in the VM, it didn't get GC'd.  You can't "intercept" an 
object from getting garbage-collected like that.

Original comment by wasserman.louis on 6 Sep 2012 at 9:42

GoogleCodeExporter commented 9 years ago
Yeah ok that's right. Whereas from within your finalizer you can promote from 
weak to strongly reachable. Was confused, thanks.

Original comment by brianfromoregon on 6 Sep 2012 at 9:56

GoogleCodeExporter commented 9 years ago
Yes...though that's one of the main reasons finalization is weird, 
unpredictable, and best avoided.

Marking as Invalid because it's not possible.

Original comment by wasserman.louis on 6 Sep 2012 at 10:13

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:13

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:08