Struggling with memory leak

21 views
Skip to first unread message

Matt Lund

unread,
Nov 30, 2010, 1:34:32 PM11/30/10
to Castle Project Users
I've read several articles about release policies, and it's only
reluctantly that I post this because I'm sure I must be doing
something dumb.

I would expect the following code to exhibit a low/constant memory
consumption. But instead, it builds very quickly to the point that
it'll run out of memory.

I think I may be having a problem getting the child container to be
released from the parent container but I'm not sure. Any ideas?

class Program
{
static void Main(string[] args)
{
IWindsorContainer parentContainer = new WindsorContainer();

parentContainer.Register(Component.For<Service1>().ImplementedBy<Service1>().LifeStyle.Transient);

for (int i = 0; i < 100000; i++)
{
using (IWindsorContainer childContainer = new WindsorContainer())
{
childContainer.Kernel.ReleasePolicy = new
LifecycledComponentsReleasePolicy();
parentContainer.AddChildContainer(childContainer);

Service1 service1 =
(Service1)childContainer.Resolve(typeof(Service1));
service1.Method1();

childContainer.Release(service1);

if (i % 100 == 0)
{
GC.Collect(2);
Console.WriteLine("Iteration: {0}, Memory {1}", i,
GC.GetTotalMemory(false));
}

parentContainer.RemoveChildContainer(childContainer);
}
}
}
}

public class Service1
{
private byte[] big = new byte[1000000];

public void Method1()
{
}
}

Mauricio Scheffer

unread,
Nov 30, 2010, 4:36:20 PM11/30/10
to Castle Project Users
Looks like an issue with child containers... what's your scenario for
choosing child containers?

--
Mauricio

Matt Lund

unread,
Dec 1, 2010, 11:02:37 AM12/1/10
to Castle Project Users
Thanks for your reply. I thought that in a server web services
application, parent/child would be the best approach to take because
the registration of components would occur just once (on the parent
container) and then a child container would be instantiated for the
lifecycle of each web service call that comes in. This avoids the
performance hit of component registration for each call that comes in.

That was my thinking but I'm new to IoC containers :)

On Nov 30, 2:36 pm, Mauricio Scheffer <mauricioschef...@gmail.com>
wrote:

Mauricio Scheffer

unread,
Dec 1, 2010, 5:36:28 PM12/1/10
to castle-pro...@googlegroups.com
With Windsor, child containers are hardly ever the "right" solution. Normally you'll have a single container per application, initialized at app startup. If you want to define the scope for your components, take a look at lifestyles: http://stw.castleproject.org/Windsor.LifeStyles.ashx

--
Mauricio



--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To post to this group, send email to castle-pro...@googlegroups.com.
To unsubscribe from this group, send email to castle-project-u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/castle-project-users?hl=en.


Matt Lund

unread,
Dec 2, 2010, 8:26:54 PM12/2/10
to Castle Project Users
Thanks Mauricio, I'll look into that.

On Dec 1, 3:36 pm, Mauricio Scheffer <mauricioschef...@gmail.com>
wrote:
> With Windsor, child containers are hardly ever the "right" solution.
> Normally you'll have a single container per application, initialized at app
> startup. If you want to define the scope for your components, take a look at
> lifestyles:http://stw.castleproject.org/Windsor.LifeStyles.ashx
>
> --
> Mauricio
>
> > castle-project-u...@googlegroups.com<castle-project-users%2Bunsu...@googlegroups.com>
> > .
Reply all
Reply to author
Forward
0 new messages