I want to write code to use Powershell and create a new session by CredSSP authentication, but when I monitor the memory usage for this console, I found something is not disposed, so the memory usage grows slowly.
After I use ANTS to check memory leak, then I was found the key point is credssp
, but I can't find the way to dispose it or clean this object.
My envionment is: .NET 3.5, PowerShell 2.0, VS 2010, Windows 7 SP1
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 10000; i++)
{
using (PowerShell powershell = PowerShell.Create())
{
powershell.AddScript("$account = \"xxx\"");
powershell.AddScript("$pwd = \"xxx\"");
powershell.AddScript("$credential = New-Object System.Management.Automation.PsCredential($account, (ConvertTo-SecureString $pwd -AsPlainText -Force))");
powershell.AddScript("$sessions = New-PSSession -ComputerName localhost -Credential $credential -Authentication Credssp");
powershell.AddScript("Remove-PSSession -session $sessions");
ICollection<PSObject> results = powershell.Invoke();
Console.WriteLine(System.Diagnostics.Process.GetCurrentProcess().WorkingSet64);
}
}
Console.Read();
}
}