3

I am stuck with the following problem: we have an app that runs an continuous loop opening remote connection via powershell (performing some actions and then closing). This works fine in a Windows machine but not in Linux ones (tested on Ubuntu 16.0.4).

Use the the script below to reproduce the problem:

$i = 0
while ($i -le 200)
{
    $password = "myPassword"
    $domain = "domain\computerName"
    $computerName = "xxx.xxx.xxx.xxx"
    
    $pwSession = convertto-securestring -AsPlainText -Force $password
    $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $domain,$pwSession
    $session = new-pssession -computername $computerName -credential $cred -Authentication Negotiate
    
    Remove-PSSession -Session $session
    Get-PSSession

    sleep 3
    $i
    $i++
}
  1. enter a powershell context by running pwsh
  2. run the script above (copy + paste)
  3. run top -p {process id} on the pwsh process id (you can run it in a single step with top -p $(ps -aux | grep pwsh | head -n 1 | cut -d' ' -f5)

You will see a window like

enter image description here

You will notice the memory consumption will keep growing (by 0.1 percent at each iteration), indefinitely.

Google does return a couple of posts mentioning memory leaks in opening new sessions with powershell, but I could not find in any of them an explanation on why the simple script above would create such an issue - again, in linux, only.

Any ideas in how tackle this issue ?

Veverke
  • 338
  • 3
  • 16
  • [This](https://github.com/PowerShell/PowerShell/issues/5244) does confirm memory leaks in powershell, have been trying identify which cache should I clear, if any – Veverke Apr 12 '21 at 07:19
  • in the bug report I can see talks about doing a loop >= 1024 rather than 201 as you do. https://github.com/PowerShell/PowerShell/issues/5244#issuecomment-341183829 – A.B Apr 12 '21 at 09:21
  • @A.B: have just started another session to test a 1030 iterations loop, will update. Thanks. Although... this will not be of any help... I will not wait that much iterations for the memory to start being freed... – Veverke Apr 12 '21 at 09:31
  • @A.B: A 1030 iterations loop did not reach a point where memory starts being freed. – Veverke Apr 12 '21 at 10:07
  • By the way, you can use `pgrep` instead of trying to parse `ps`: `top -p $(pgrep -f pwsh) `. – terdon Apr 12 '21 at 10:59
  • @terdon: nice, at least am not leaving empty handed here :-) – Veverke Apr 12 '21 at 11:03
  • Whoops, sorry, I had a typo (corrected now). It should be `pgrep -f` not `pgrep pf` as I had. Anyway, I suppose that simply not using powershell isn't an option, right? – terdon Apr 12 '21 at 11:06
  • Moreover, even after the loop with 1030 iterations ends memory usage keeps increasing (although in a slower pace) – Veverke Apr 12 '21 at 11:06
  • And is this actually a problem? I mean, does it cause any issues? It could just be the disk cache or something similar and the memory will be released as soon as any program asks for it. See http://linuxatemyram.com/ – terdon Apr 12 '21 at 11:09
  • There's no need for top. use ps, that's what it's for. e.g `ps u -C pshw` (or `ps v -C pshw`, or print only the fields you want with `-o`). – cas Apr 12 '21 at 11:09
  • @cas the point here is that the memory use changes over time, so `top` is a natural choice and simpler than running a `ps` with `watch`. – terdon Apr 12 '21 at 11:10
  • @terdon: For the time being, yes, not an option. I understand things are expected to work better using `ssh`, but currently there are no plans to use it. I am reading yout 'ate my ram' link. – Veverke Apr 12 '21 at 11:29

0 Answers0