Posh-SSH icon indicating copy to clipboard operation
Posh-SSH copied to clipboard

Invoke-SSHCommand / Stream raises memory usage of powershell in a loop

Open Dartore opened this issue 2 years ago • 8 comments

Hi there,

I tried a lot of stuff to solve this issue with powershell commands and system commands (GC), but couldn't resolve this. When used in an infinity loop the memory usage raises with no limit. Did already every use of invoke against in my script against $null and let [GC] clear this, but the module still keeps the memory.

In my case there is a loop every 10 seconds with this: $null = (Invoke-SSHCommandStream 0 -Command "tail -n 2000 /log/Test.log") | Out-File -FilePath $Snapx So every 10 seconds the memory of the powershell process gets bigger and bigger. (with my log the 2000 lines are about 5MB) For better testing the lines can be 10000 or 20000.

Maybe there is a tweak for the module.

br

Dartore avatar Nov 20 '23 11:11 Dartore

Will take a look, why not use a monitoring solution with syslog?

darkoperator avatar Nov 20 '23 12:11 darkoperator

Sadly there is no other way to do so, on the hard and software behind the needed log. (no changes on the system allowed) Thnx for looking.

Dartore avatar Nov 20 '23 12:11 Dartore

So that I can test replication properly ehat version of PS and are you using the latest module?

darkoperator avatar Nov 20 '23 12:11 darkoperator

Windows Server 2019 PS 5.1.17763.4974 Module 3.0.8

Dartore avatar Nov 20 '23 12:11 Dartore

Can you try 3.1.1 of the module? It is using an updated version of renci.ssh that addresses many bugs

darkoperator avatar Nov 20 '23 13:11 darkoperator

No, 3.1.1 don't help. It seems, that every tick loads the amount of lines into the $Result or $Reader of the Invoke-SSHCommandStream and won't get cleared after. (it's a guess)

Dartore avatar Nov 20 '23 14:11 Dartore

In a process using a stream even by recurring [System.GC]::Collect() and dispose of the stream the memory used by it stays. Tried with the command and the ssh.net library straight calls by creating a loop that ran dmesg 10000 waiting 2 seconds to collect the info,. now, with that, I was able to grow it by a couple of hundred megs in a couple of hours. TO be honest I would not recommend a infinite loop with a stream, you could use jobs to pull the data and that information once ran it will kill the thread/process of the job freeing up the memory. But given my limited .net skills no clue how not to prevent this scenario

darkoperator avatar Nov 21 '23 13:11 darkoperator

Thnx for checking, i'm trying to reload the module after some time.

Dartore avatar Nov 21 '23 15:11 Dartore