jborean93/PSWSMan

Memory leak in Invoke-Command over PSWSMan

Closed this issue · 2 comments

Environment:

Name                           Value
----                           -----
PSVersion                      7.3.5
PSEdition                      Core
GitCommitId                    7.3.5
OS                             Linux 5.14.21-150400.24.100-default #1 SMP PREEMPT_DYNAMIC Mon Dec 4 19:12:13 UTC 2023 (3f5cd84)
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Steps to reproduce

  1. Create a New-PSSession to a remote PC
  2. Do any Invoke-Command on the PSSession
  3. Run Garbage Collectors to release as much memory as possible
  4. Run steps 2 and 3 multiple times, the memory keeps on increasing with the number of executions.

Sample code:

$Username = "username"
$Password = "password"
$HostName = "hostname"
$pwd = ConvertTo-SecureString $Password -AsPlainText -Force
$HostCred = New-Object System.Management.Automation.PSCredential ($Username, $pwd)
$HostSession = New-PSSession -ComputerName $HostName -Credential $HostCred -Authentication Kerberos -ErrorAction Stop
if(! $?) {
    Write-Host "Host session cannot be established!"
    exit 1
}

function  MemoryLeakTest{
    param (
        $HostSession
    )
    while ($true) {
        $p = Invoke-Command -Session $HostSession -ScriptBlock { Get-Process PowerShell }
        $p = $null
        [System.GC]::Collect()
        Write-Host "Memory leak test is ongoing"
        Start-Sleep 10
    }
}


MemoryLeakTest $HostSession

###Expected behavior

Memory consumption should reach a limit, and not increase thereafter.

###Actual behavior

Memory consumption keeps on increasing with the number of executions.

What version of PSWSMan are you using, is it this repo built manually or the PSWSMan version on the gallery? If it's the latter that's actually a build of my omi fork which is essentially a modified version of Microsoft's omi C code. I've stopped working on that in favour of this unreleased repository due to various issues like memory leaks.

Closing as per the above.