I see the problem in PS v3.0, but not in PS v2.0. Here is the code that I use to see this (all examples are in PowerShell):
for() { $runspace = [runspacefactory]::CreateRunspace() $runspace.Open() $runspace.Close() $p = Get-Process -Id $PID '{0} {1}' -f $p.Handles, ($p.PrivateMemorySize / 1mb) }
Pens and memory seem to leak in version 3.0 in the code above.
Since v2.0 does not have this problem, one possible workaround is to start the service using PS v2.0, i.e. PowerShell.exe -Version 2.0 .
If this is not possible, I can come up with two more workarounds. One of them is not for creating spaces directly, but use [powershell] instead. For example, this code does not show a leak in version 3.0:
for() { $ps = [powershell]::Create() $p = $ps.AddCommand('Get-Process').AddParameter('Id', $PID).Invoke() '{0} {1}' -f $p.Handles, ($p.PrivateMemorySize / 1mb) $ps.Dispose() }
Another workaround, if applicable, could be to use [runspacefactory]::CreateRunspacePool() . This method also does not show Leak:
$rs = [runspacefactory]::CreateRunspacePool() $rs.Open() for() { $ps = [powershell]::Create() $ps.RunspacePool = $rs $p = $ps.AddCommand('Get-Process').AddParameter('Id', $PID).Invoke() '{0} {1}' -f $p.Handles, ($p.PrivateMemorySize / 1mb) $ps.Dispose() }
The latter also works much faster because the space bar repeats the use.
Roman kuzmin
source share