Repository navigation
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.


Activity
iSazonov commentedon Dec 27, 2019
Can you repo with latest PowerShell 7 build?
RainbowMiner commentedon Dec 27, 2019
Good question, I don't have a pwsh 7 install, yet. I will try this first thing, tomorrow morning.
RainbowMiner commentedon Dec 27, 2019
One additional remark: the leak also happens, when using
-first,-last,-indexetc. on Select-Object, but does not happen, when using-Propertyor-ExpandProperty. This might be, because the latter two create new objects.[-]Memory leak when Select-Object on $Global scope array of PSCustomObject[/-][+][Bug in PS 5,6 and 7] Memory leak when Select-Object on $Global scope array of PSCustomObject[/+]RainbowMiner commentedon Dec 28, 2019
Yes, same thing happens with PowerShell 7, too.
iSazonov commentedon Dec 28, 2019
Perhaps it is related #7768
RainbowMiner commentedon Dec 30, 2019
It's a pretty severe bug which did cost me many hours of searching. I had never suspected such a basic function like select-object to cause this much trouble. If you have a long running script, that does select-object on a global PSCustomObject array e.g. every 60 seconds, it will fill up all your machine's ram within 24 hours.
Regarding issue #7768, I stumbled upon this issue during my search for the memory leak. Actually, that one addresses the use of
-ExpandPropertyand-Property- if using select-object with one or both of those parameters, the memory leak does not happen. Most probably, because a new object will be forwarded through the pipeline.It's happening only, when using blank select-object or positional select-object, which should simply forward a selection of the original objects
iSazonov commentedon Dec 31, 2019
I can not repo on PS 7.0. GC works great.
I tried:
RainbowMiner commentedon Dec 31, 2019
For me, it doesn't work out.
$PSVersionTable
Before:
Now I run this code:
After:
Now I run this code again:
I am using the following function to get the memory usage. There is no change to the above values, if I would call [System.GC]::Collect() one or multiple times.
iSazonov commentedon Dec 31, 2019
What is
$Global:last_memory_usage_bytein your script? I don't see that the variable is assigned.RainbowMiner commentedon Dec 31, 2019
Ah, that's being set to 0 at the start and set within the above function. I had stripped some code from the Get-MemoryUsage function, before posting it here. I unintentionally did cut out the reset line (I just re-added it)
I did another 100 runs:
Now the memory is here:
RainbowMiner commentedon Dec 31, 2019
The memory dump on Pwsh 7 is a bit different, than that I aquired from the Pwsh 6 dumps:
Tons of strings of type
System.Collections.Generic.List<string>Tons of strings of type
System.Management.Automation.Runspaces.ConsolidatedStringSo, basically a similar result, compared to PS6, but in a different order and objects. But it all comes down to a giant collection of strings with the value
"Selected.System.Management.Automation.PSCustomObject"5 remaining items
lzybkr commentedon Jan 2, 2020
Ilya (@iSazonov) - if the cmdlet is passing objects through unmodified - it doesn't make sense to add the prefix, so I think the condition was written with that intent as it creates custom objects from the selected properties.
iSazonov commentedon Jan 3, 2020
Jason Shirk (@lzybkr) It is not clear why we add the prefix at all. Using Select-Object implies that this cmdlet always creates custom objects (except in edge bypass case). Also adding the type prefix is not documented. So I think we could remove the feature.
lzybkr commentedon Jan 4, 2020
Ilya (@iSazonov) I don't know the history but removing the prefix would be a breaking change and it might be hard to search for (125000 hits on the word Selected in GitHub - most probably not related but some might be.)
iSazonov commentedon Jan 4, 2020
Yes, formally it is a breaking change. I found some scripts using the feature https://github.com/search?q=Select-Object+Selected+pstypenames&type=Code - there are only a few.
I think PowerShell Committee can already make a conclusion. /cc Steve Lee (@SteveL-MSFT)
My arguments for removing the feature:
bpayette commentedon Jan 8, 2020
Ilya (@iSazonov)
Not specifically but as a general rule, when PSCustomObjects are produced, information is added to TypeNames so you can identify the origin of the object. So this is an entirely reasonable behaviour.
No it's not. You showed that it would absolutely break people.
It creates custom objects because these are projections - there is no nominal type to return. It might be possible to do what LINQ does: synthesize anonymous types and unify on property names but that would be a breaking change and I'm not sure generating transient types is the best solution for this scenario. It must be possible to optimize this (used interned strings or something.)
Since you're ultimately doing the same thing, how is that going to fix the problem?
SteveL-MSFT commentedon Jan 8, 2020
@PowerShell/powershell-committee reviewed this, we agree that simply removing the
Selected.type IS a breaking change so any fix would need to be made without breaking existing users.iSazonov commentedon Jan 9, 2020
Th problem is that users in 99.999% cases do not need/not use (how GitHub search shows) and do not know the feature, and if they works with long time live custom object they can catch the issue with leak. In other words, it should be opt-out or just delegated to users.
This is all the more amazing because we do nothing of the kind in Where-Object, Foreach-Object,
and specially in Add-Member.
bpayette commentedon Jan 9, 2020
To be clear,
Select-Objectshould only be adding theSelectedprefix toPSCustomObjectsit projects (creates). Everything else is a bug. Adding it 10000 times is a bug. Adding it to objects that it did not project is a bug. Does this make sense?