I have a windows service that is doing a lot of exchange remote calls to get some server information. I noticed that as longs as the time passes the memory used by the service starts growing until a memory exception is thrown. I have searched and it looks like there is a known memory leak in the System.Management.Automation
that does not dispose all the memory of the Runspace
created while calling the close and/or dispose method. I reviewed a post that suggest using the CreateOutOfProcessRunspace
of the RunspaceFactory
but not sure how to use it.
Here is how the issue can be reproduced: (System.Management.Automation
dll referenced)
for (int i = 0; i < 1000; i++)
{
var runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
runspace.Close();
runspace.Dispose();
}
If you run this code, you will see how the memory is incremented. Due to the requirements, keeping a connection open as much as possible is not a good solution.
Do you know how I can fix this issue, even using the CreateOutOfProcessRunspace
method of the RunspaceFactory
or how to propery dispose the memory?
Thanks in advance
EDIT
I was using the V3 and change the runspace creation to use the CreateRunspacePool method and it looks like the leak is gone. Thanks so much for your help!