I’m getting unexpected slowdowns when processing a very large Array in PowerShell.
I use Get-ChildItem to obtain an Array of FileInfo objects; the resulting Array has about 75,000 elements.
I then start uploading the files with a foreach ($file in $queue)
construct, passing the file name to WinSCP’s .NET Assembly. After each file is uploaded, I clear its archive bit.
I’m uploading the files via SFTP to an OpenSSH server running on localhost on a solid state hard drive. No Internet access involved—everything is in memory.
The script runs fine at first (I estimate about 5 files uploaded per second), but after processing 10–20,000 files it begins to slow down to a crawl. It eventually gets so slow it appears frozen, and and I have to kill & restart the script. As soon as I restart the script, it runs (initially) at its original zippy speed.
The slowdown occurs both when running in Visual Studio Code, and in a standalone instance of PowerShell. It also persists after a system reboot.
I don’t touch the array itself during the process, and system memory use never gets above 30-35% (I have a 64-bit system with 16 GB RAM).
Does PowerShell have any known memory management problems with large data sets? Would using an ArrayList and deleting each element after its file is uploaded make things any faster?
Reproducing the problem takes several hours of runtime before the slowdown becomes unbearable, so I thought I’d see if anyone has experienced this kind of problem before I resort to brute force testing. And, does anyone know how long it would take to create a 75,000 element ArrayList from an Array?
Any insights would be greatly appreciated.
My $PSVersionTable:
Name Value
---- -----
PSVersion 7.3.6
PSEdition Core
GitCommitId 7.3.6
OS Microsoft Windows 10.0.19045
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0