2
votes

I'm looking for a way to sneak a call back function whenever an object reference has been garbage collected.

I know I can wrap the object in a weak reference, but I would still have to poll a collection of references for status change.

I would prefer not to poll as that seems like a waste of CPU cycle when nothing happens. Is there a more effective method to detect when an object has been garbage collected?

Note: I do not have access to the object code and hence would not be able to add the call back in the finalizer.

1

1 Answers

0
votes

This might not be the answer you are looking for, but there is no callback when the object is collected. If you don't have access to the class, you might be able to wrap it in a class that has a finalizer and use that instead.