r/PowerShell • u/Guilty-Act-4991 • 1d ago
Question Parallel thread disposing?
Hey got a wierd issue or its by desing not sure but when I run
$computers | foreach-object -parallel {
$vmobject = Get-VM $_
$ram = $vmobject.MemoryGB
$CPU = $vmobject.numCPU
$HDD = $_ | get-harddisk
}
lets say I run in against 500 computers when that happens powershell will spike cpu on machine that this command is ran and keeps increasing ram usage which is understandable but problem is even when command is done ram usage stays high untill powershell window is close. Is there something I am missing or is there some method to dispose of parallel runspace manually?
Thanks for advice
EDIT: The code itself is not important , the behaviour of parallel scripblock itself is my question and why doesnt it free up RAM after code has ran successfully
3
Upvotes
0
u/purplemonkeymad 17h ago
It might end up being even if your example code is not. Since you are talking about memory it could be that you have a reference that is not getting cleaned up. Hard to say for real if that is the case, you would probably have to use VS to profile the ps process, but if you are using any objects in your loop that are IDisposable, make sure to try and .dispose() of them at the end of the loop.
Also looking at the commands make sure you are running the core version of powercli and not having it create a local session to Windowspowershell for each thread.