Is there a way in Unity to trace (maybe System.Diagnostics TraceListener) the instance lifecycle?

Oct 24, 2012 at 6:04 PM
Edited Oct 24, 2012 at 6:05 PM

I'm trying to diagnose the creation of instances that implement IDisposable within a HierarchicalLifetimeManager which are ending up in the Gen2 heap and don't appear to ever be garbage collected. Does Unity implement any kind of tracing capability to show when Unity is creating and disposing instances registered in a container?

Oct 24, 2012 at 6:45 PM

I don't believe there is any tracing in Unity.  You could modify the source to add whatever tracing you are interested in.  Also you could make a suggestion for the next version, which is in planning stages:

Randy Levy
Enterprise Library support engineer 

Oct 24, 2012 at 7:58 PM

Do you think it would be easier to use some of the existing extension points in Unity or event handlers I could hook to instead of hacking the source for tracing?Thanks for the quick reply!

Oct 25, 2012 at 1:05 AM
Edited Oct 25, 2012 at 1:17 AM

Is the container disposed?

If not nothing will be telling your instances to Dispose unless you have done it manually.  Even so, the container still holds a reference to the lifetime manager which holds a reference to your instance which will stop the GC from collecting the object?

The HierarchicalLifetimeManager is conceptually the same as the ContainerControlledLifetimeManager as to the lifetime of the instances.

Transient and PerResolve - The top level object resolving the dependency chain owns the lifetime of the instance and therefore the responsibility to Dispose.  The GC will collect once instances are out of scope.

ContainerController and Hierachical - Containers own the lifetime and therefore dispose.  The GC will not collect unless the container is disposed.

PerThread and ExternallyControlled - All bets are off.  Chances are the GC will collect your object so you need to implement a Finalizer to remove unmanaged resources.