inhibit garbage collection
-
im curious and unsure (and wading through a fog of nondeterministic destruction)... if one was to obtain a
System.Runtime.InteropServices.GCHandle
for each reference field of a class, and that class becomes eligible for finalization (implying that all its internal referenced objects also became eligible), would that class be finalized before any of its references (for sake of argument, the GCHandles were stored say, in an array in another reference type and both the array and its host object were also GCHandle'd in the array) ? would it be safe to assume all reference fields are still accessible at finalization time, allowing a controlled cleanup ? how might this be affected when the AppDomain unloads or CLR exits ? would theSystem.Environment.HasShutdownStarted
property be of any use/relevance ?
I worship his divine shadow. ^
-
im curious and unsure (and wading through a fog of nondeterministic destruction)... if one was to obtain a
System.Runtime.InteropServices.GCHandle
for each reference field of a class, and that class becomes eligible for finalization (implying that all its internal referenced objects also became eligible), would that class be finalized before any of its references (for sake of argument, the GCHandles were stored say, in an array in another reference type and both the array and its host object were also GCHandle'd in the array) ? would it be safe to assume all reference fields are still accessible at finalization time, allowing a controlled cleanup ? how might this be affected when the AppDomain unloads or CLR exits ? would theSystem.Environment.HasShutdownStarted
property be of any use/relevance ?
I worship his divine shadow. ^
The first question would be, why would you want to prevent the garbage collection from running?
mikewinny wrote:
would that class be finalized before any of its references
I don't think so. The runtime doesn't guarantee when the objects will be collected and it doesn't provide any guarantees on the order the collection will occur.
mikewinny wrote:
would it be safe to assume all reference fields are still accessible at finalization time
If you are writing your own finalizer, you shouldn't be accessing other finalizable fields. Again, since the runtime doesn't guarantee any ordering, I would say that it isn't safe to make that assumption. The
Environment.HasShutdownStarted
property will tell you if your finalizer has started to run, but not much more than that. It is also only available in .NET 2.0 and later. Check out the latest updates to my article Implementing IDisposable and the Dispose Pattern Properly[^]. Hopefully, it will answer these questions in a little more detail. You may also find some additional answers here: SafeHandle and Constrained Execution Regions[^]