java - Garbage Collector Listeners -
i read small article @ point adding callbacks weakreference objects triggered upon garbage collection. now, no mater how search, cannot find it.
i need way execute code whenever weak referenced object destroyed. know can done, don't remember how or whether need weakreference or else weakhasmap?
use referencequeue
s archieve that. might want phantomreference
s, too, depending on you're trying (but weakreference
s work queues, too). create reference queue , pass second argument reference-constructor. when gc remove object, reference enqueued , can using remove()
(blocking) or poll()
(non-blocking) on queue.
there alternative: implementing finalize
. it's less flexible though , runs in thread, (so still have concurrency - addition of not knowing thread execute it). referencequeue superiour in aspects.
Comments
Post a Comment