Is there any possibility to detect if an object is reachable and can
be collected by the GarbageCollector?
I'm using a cache of weak references to persistent objects. Objects
are then accessed using an ObjectID. If the object is still alive, the
object is retrieved from the cache, if not, it will be fetched from a
database and the referece is returned to the caller.
Now I'm trying to implement a kind of automatic pessimistic locking:
when an object is placed into the cache, to object is locked. The
caller can now modify the object. After a defined period of time, I
would like to search the cache and detect the objects wich are not
referenced any more. These objects can then be unlocked and can be
modified by other users.
One possibility would be to call the GC.Collect(), walk the cache and
find all weak references which have been collected. Using the objectId
it is now possible to unlock the object in the database.
Unfortunatelly calling GC.Collect() is probably not a good idea. Any
other suggestions? As far as I know the GC performs a collect in two
steps: mark and sweep. Is it possible to execute just the mark step
and use the result for detecting if an object is still reachable??
Thanks,
Gerhard.