TypedFactory vs Performance Monitor

205 views Asked by At

I'm confused about how new windsor3 perfmonace counter shows tracking of objects generated via TyepedFactory.

considering following scenario

public interface IBFactory
{
    IB[] GetAll();
    void FreeUp(IB cmps);
}

public class B1 : IB, IDisposable
{
    public void Add(int i){}

    public void Dispose()
    {
        Console.WriteLine("Disposing " + GetType().Name);
    }
}

public class B2 : IB, IDisposable
{
    public void Add(int i){}

    public void Dispose()
    {
        Console.WriteLine("Disposing " + GetType().Name);
    }
}

public class B3 : IB
{
    public void Add(int i){}

    public void Dispose()
    {
        Console.WriteLine("Disposing " + GetType().Name);
    }
}


 var container = new WindsorContainer();

 var diagnostic = LifecycledComponentsReleasePolicy.GetTrackedComponentsDiagnostic(container.Kernel);
 var counter = LifecycledComponentsReleasePolicy.GetTrackedComponentsPerformanceCounter(new PerformanceMetricsFactory());
 container.Kernel.ReleasePolicy = new LifecycledComponentsReleasePolicy(diagnostic, counter);

 Console.WriteLine("Enter number of iterations:");
 int iterations = int.Parse(Console.ReadLine());

 container.AddFacility<TypedFactoryFacility>();
 container.Register
 (
    Component.For<IBFactory>()
        .AsFactory()
        .LifeStyle.Transient,

    Classes.FromAssemblyContaining<IB>()
        .BasedOn(typeof(IB))
        .WithService.Base()
        .Configure(c => c.LifestyleTransient())
 );

 Console.WriteLine("Create Memory Leak Y or N?");
 var leak = Console.ReadLine().ToUpper() == "Y";


 var sleepFor = 100;// int.Parse(Console.ReadLine());

 for (var i = 1; i < iterations+1; i++)
 {
    var factory = container.Resolve<IBFactory>();
    Console.WriteLine("Factory created.");

    var cmp = factory.GetAll();
    foreach (var b in cmp)
    {
        b.Add(i);
    }

    Console.WriteLine("Iteration {0} completed", i);

    Thread.Sleep(sleepFor);

    if (!leak)
    {
        foreach (var b in cmp)
        {
            factory.FreeUp(b);
        }
    }

    Console.WriteLine("Releasing factory.");
    container.Release(factory);     
 }

 Console.WriteLine("container disposing.....");
 container.Dispose();
 Console.WriteLine("container disposed");
 Console.ReadLine();

If I dispose objects, as I should, via FreeUp factory method, perf counter shows expected tracking.

Instead if I do not expliclty dispose objects, but if I'll do implicitly disposing the factory, created as transient for testing purpose, IB instances are disposed when I dispose the factory (as per documentation), but perf counter does not get updated and shows IB instance still as tracked...

What that means? Perf counter has not been updated or objects are still tracked(that's would be very scary) even if Dispose has been called on IB instances due to factory disposing.

0

There are 0 answers