-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Description
Steps to reproduce
function Initialize-Something {
$Global:TestArray = @()
$Global:TestArray += [PSCustomObject]@{abc=1;def=2}
$Global:TestArray += [PSCustomObject]@{abc=2;def=3}
$Global:TestArray += [PSCustomObject]@{abc=3;def=4}
}
function Invoke-Something {
for($i=0; $i -lt 1000; $i++) {
$Global:TestArray | Select-Object > $null
}
}
Initialize-Something
Invoke-Something
Expected behavior
Garbage collection should clear heap afterwards
Actual behavior
Every call to Invoke-Something collects more objects of the following type in managed memory:
{System.Collections.Concurrent.ConcurrentDictionary<string, System.Management.Automation.PSMemberInfoInternalCollection<System.Management.Automation.PSMemberInfo>>.Node}
Most notably, the "_key" contains the following.
"Selected.System.Management.Automation.PSCustomObject@@@Selected.System.Management.Automation.PSCustomObject@@@Selected.System.Management.Automation.PSCustomObject@@@Selected.System.Management.Automation.PSCustomObject@@@Selected.System.Management.Automation.PSCustomObject@@@Selected.System.Management.Automation.PSCustomObject@@@Selected.System.Management.Automation.PSCustomObject@@@Selected.System.Management.Automation.PSCustomObject@@@Selected.System.Management.Automation.PSCustomObject@@@Selected.System.Management.Automation.PSCustomObject@@@Selected.System.Management.Automation.PSCustomObject@@@Selected.System.Management.Automation.PSCustomObject@@@Selected.System.Management.Automation.PSCustomObject@@@Selected.System.Management.Automation.PSCustomObject@@@Selected.System.Management.Automation.PSCustomObject@@@Selected.System.Management.Automation.PSCustomObject@@@Selected.System.Management.Automation.PSCustomObject@@@Selected.System.Management.Automation.PSCustomObject@@@Selected.System.Management.Automation.PSCustomObject@@@Selected.System.Management.Automation.PSCustomObject@@@System.Management.Automation.PSCustomObject@@@System.Object"
After each run of Invoke-Something, every newly created object of the above type seems to inherit this list, but increased by one more Selected.System.Management.Automation.PSCustomObject. This creates an exponential growth, over time.
Environment data
Name Value
---- -----
PSVersion 6.1.0
PSEdition Core
GitCommitId 6.1.0
OS Microsoft Windows 10.0.18362
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
.. but the same happens with pwsh core 6.2.3, as well. Same thing happens, when running pwsh on a linux platform.

