Skip to content

[Bug in PS 5,6 and 7] Memory leak when Select-Object on $Global scope array of PSCustomObject #11441

@RainbowMiner

Description

@RainbowMiner

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}

image

image

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Committee-ReviewedPS-Committee has reviewed this and made a decisionIssue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aWG-Cmdlets-Utilitycmdlets in the Microsoft.PowerShell.Utility module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions