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 referencequeues archieve that. might want phantomreferences, too, depending on you're trying (but weakreferences 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

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -