Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to return all unrefrenced objects?

I have this "Linked Web"(?) data structure ie. each object has a bunch of references to other objects.

So i wrote a method that is supposed to 'remove' a passed object by removing all references to it.

I need to test it and make sure that after i run the method the particular object i want to remove is not referenced by anything else

How could I do this?

An idea would be to force a Garbage Collection then run my delete object method and then force another GC to see if it found an object.

if it found an object for deletion then I would assume that my method works but if it found nothing to collect then I would assume that something is referencing it and would have to plug that leak

Is this possible? How?

Thanks, Ryan

like image 714
Ryan Lee Avatar asked Dec 06 '25 23:12

Ryan Lee


1 Answers

You can keep track of each object via a WeakReference and check the IsAlive property after a garbage collection.

like image 137
Mark Cidade Avatar answered Dec 08 '25 11:12

Mark Cidade



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!