I am attempting to write a resource manager that will eventually release a resource when it is no longer being used.
Unfortunately, as the manager itself always keeps a reference to the resource object, even after no other code in the program refers to it, the garbage collector will not free it. Is there a way to find out how many references there are to an object instance, or barring that a set of classes in C# that are already implementing the behavior I'm looking for? It seems silly to write my own reference counting into the code when C# is already doing it for you.
Thanks for any help.
Loading
AlanPosted Jan 31, 2008, 4:01 PM
I'm virtually certain that this isn't possible because even the garbage collector itself doesn't know how many references there are to an object without 'walking the heap' and would stop in any case as soon as it found one.
Moreover, there's no method in the GC class which enables you to tell the garbage collector to walk the heap for a particular object and find out how many references there are to it.
Dr SpackPosted Jan 30, 2008, 3:38 PM
I do not know how to count the references, but maybe a look at System.WeakReference does give you an other area to search.
(And if you find a way to count, let us know.)