I take a chance to expose
a wish I have for quite some time. I would like the following method in the
T GetAliveInstancesOf<T>() where T : class
By alive I mean all
instances reachable from the GC roots. I am not a GC specialist but I am pretty
sure that this method wouldn’t be that hard to develop. Btw, SOS provides
this feature already.
With such method one would be
able to check at any time that all instances of a type satisfy some conditions.
More specifically I would like to check periodically that some code doesn’t
have memory leak by verifying some properties on the objects. For example, in the
NDepend code base we
use some kind of composite patterns. With such API, it would be easy to check
that all alive leaf instances are part of a valid graph (by checking recursively all
parents of all objects).
method could come at a
performance cost because it would certainly forces the GC to stop all thread to
build the list of alive instances. But all these checks would be done at
development/debug/test time and removed from production code.
I imagine that such
method could be hi-jacked by some programmers for awkward use. But the core
part of the framework contains already plenty of sensitive thinks that can easily be hi-jacked.