PowerShell
PowerShell copied to clipboard
Get-Volume Memory Leak
Prerequisites
- [X] Write a descriptive title.
- [X] Make sure you are able to repro it on the latest released version
- [X] Search the existing issues.
- [X] Refer to the FAQ.
- [X] Refer to Differences between Windows PowerShell 5.1 and PowerShell.
Steps to reproduce
I am hosting PowerShell in a C# application using the Microsoft.PowerShell.SDK package. The application periodically calls Get-Volume. Memory usages steadily climbs as it runs throughout the week. Only restarting the entire application/process clears the memory.
Reproduction Code:
using System.Diagnostics;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
int count = 100;
Console.WriteLine($"Running Get-Volume {count} times...");
for (int i = 0; i < count; i++) {
InitialSessionState iss = InitialSessionState.CreateDefault();
iss.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.Unrestricted;
using (var ps = PowerShell.Create(iss)) {
ps.AddScript($"$Result = (Get-Volume | Select -First 1).Size");
ps.Invoke();
var resultVar = ps.Runspace.SessionStateProxy.PSVariable.GetValue("Result");
Console.WriteLine($"Iteration Output: {resultVar}");
}
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
Console.WriteLine();
Console.WriteLine($"Memory Usage: {Process.GetCurrentProcess().PrivateMemorySize64 / 1024 / 1024} MB");
Console.WriteLine();
Console.WriteLine("Finished. Press any key to exit.");
Console.ReadKey();
Expected behavior
Memory clears up as garbage collection runs.
Actual behavior
Memory usage steadily increases.
Error details
N/A
Environment data
Name Value
---- -----
PSVersion 7.2.5
PSEdition Core
GitCommitId 7.2.5
OS Microsoft Windows 10.0.19044
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Visuals

Is Get-Volume a root of the issue? Can you reproduce the issue with another cmdlet/module? Also I don't see how your repro code could show memory leak.
I have tried many other cmdlets (e.g. Get-Service, Get-CimInstance, Get-ItemProperty, etc) and Get-Volume appears to be the only one I've come across that has this issue.
The repro code shows that memory isn't cleared even though PowerShell object is disposed via the using block. You could replace Get-Volume with a different Cmdlet and see. I supposed I could have it do that first to demonstrate the difference in memory usage.
This is a similar report as https://github.com/PowerShell/PowerShell/issues/14779. Both cmdlets are from the Storage module, which is implemented as a CDXML module (meaning they involve native code execution by WMI providers).
As my previous investigation shown, the increasing committed memory was from native heap, and the size of managed heap almost didn't change over the 4 hours of my test run.
However, we talked with the owner team, but didn't get much traction on this.
I had a feeling it would effect multiple cmdlets that interact with the storage system. I figured it had to be something with native calls since no amount of disposing and forced garbage collection could clear it. Thanks for the confirmation!
When my windows service that typically consumes less than 100MB of memory started consuming multiple GB of memory it was fairly obvious there was a leak somewhere. I have since moved all PowerShell execution into it's own process using named pipes to relay results back so that I can free up memory by terminating the external process.
@StevenBucher98 and @theJasonHelmick, can we contact the owner team of the Storage module about the memory leak? This is the 2nd report about memory leaks when using cmdlets from the Storage module.
Thanks to @StevenBucher98 for getting in contact with the owner team of the Storage module, we got response from the owner team that they can reproduce the leak with Get-PhysicalDisk (they are working on a fix), and they are running tests with Get-Volume.
Close as external.
This issue has been marked as external and has not had any activity for 1 day. It has been be closed for housekeeping purposes.