0

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();
    }
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Jim Wu
  • 21
  • 1
  • `powershell.Dispose()`? – Bacon Bits Jan 23 '15 at 16:10
  • [Looks like another symptom of this bug](https://connect.microsoft.com/PowerShell/feedback/details/613730/powershell-remoting-through-runspace-open-with-credssp-authentication-leaks-memory-if-an-exception-is-thrown) – Mathias R. Jessen Jan 25 '15 at 14:08
  • hi Bacon, thanks for your response, but I already use "using", it should be dispose automatically, actually I already tried to add "powershell.Dispose()", but it's not work too. – Jim Wu Jan 26 '15 at 02:28
  • hi Mathias, yes, I think it's almost the same condition, but even there is no exception, it's still memory leak. lol – Jim Wu Jan 26 '15 at 02:31
  • Check the processes in task manager and see if any of the children are still running. The using should dispose all objects. So my best guess is one of the commands are still running. – jdweng Jan 10 '23 at 10:00

0 Answers0