Open milope opened 10 months ago
Adding a bit more. This is my memory usage a bit after:
Get-AzStorageBlob -Container test -Context $context -IncludeVersion -IncludeDeleted | Remove-AzStorageBlob -Force
PS> [Diagnostics.Trace]::Listeners.Messages.Count 31418865
Thanks for reporting and the detailed investigation. I'm locating the root cause. Just to double check:
Recommendations I would recommend avoid storing Debug messages to these ConcurrentQueue
objects if Debug output is not enabled. > Also once the Debug output has been streamed to console or destination, maybe the Queues can be cleared.
What did you refer to by ConcurrentQueue<String> objects
Likely this one in particular. I managed to dump a 4GB + dump and the memory analysis on VS pointed at that object consuming a lot of memory. I dumped some of the objects and it’s full of request and responses from the Storage SDK as if a trace injection started picking up all Azure-Core events and storing it in those queues.
Description
When running Az Storage modules that run as multi-threaded, they exhibit a memory-leak. NOTE: I did not use
$DebugPreference="SilentlyContinue"
deliberately as this is how the memory-leak is exhibited. What appears to happen is that The Storage Cmdlets appear to enable Diagnostic Tracing. It appears that diagnostic tracing is enabled on the PowerShell process and a diagnostic trace injection happens. Due to this configuration, allOutputStream.WriteDebug
calls get appended endlessly to the diagnostic tracing's Messages properties. This includes all request and response content from the .NET SDK. The more the repro is run, the larger the messages and other TaskOutputStream queues get.See below where a quick 500 iterations would pile up 54270 strings in the Messages property alone. Continue running iterations and monitor that property grow larger and larger. Please note: The
-Debug
flag is not set and the$DebugPreference
is set toSilentlyContinue
Workaround
A quick workaround I've been forced to do is to call
[Diagnostics.Trace]::Listeners.Clear()
right before and after any long-running background Storage cmdlet like the following:Debugging
When opening a 1GB memory dump running code similar to above, I found that Azure.Core.Diagnostics.AzureEventSourceListener had an inclusive size of well over 400 MB:
First Path to Root below:
Recommendations
I would recommend avoid storing Debug messages to these
ConcurrentQueue<String>
objects if Debug output is not enabled. Also once the Debug output has been streamed to console or destination, maybe the Queues can be cleared.Issue script & Debug output
Environment data
Module versions
Error output