If you are using dbatools and concerned about huge amount of memory usage, here’s some tips.
dbatools stores logs and error messages in an in-memory queue.
By default, this is enabled and will store up to 1024 log messages and 128 error messages. Because the log messages store so much information, you will see memory increase quickly. This is with 1024 messages in the queue.
If you have multiple dashboards running, long running scripts, and APIs using dbatools, each one will default to 1024 log messages and consume about this amount of memory per process.
As a test, I am running the script below every 10 seconds. It takes less than 10 minutes to reach this memory usage. I’m using the Integrated environment so that’s why the Universal.Server.exe process is using all the memory. You may see the memory usage in external PS processes.
This script prints the sizes of all the queues of messages. Finally, it invokes a simple query.
[Sqlcollaborative.Dbatools.Message.LogHost]::MessageLogEnabled = $true
[Sqlcollaborative.Dbatools.Message.LogHost]::ErrorLogEnabled = $true
$ErrorRecords = [Sqlcollaborative.Dbatools.Message.LogHost].GetField("ErrorRecords", [System.Reflection.BindingFlags]::Static -bor [System.Reflection.BindingFlags]::NonPublic)
$ErrorRecords.GetValue($null).Count
$LogEntries = [Sqlcollaborative.Dbatools.Message.LogHost].GetField("LogEntries", [System.Reflection.BindingFlags]::Static -bor [System.Reflection.BindingFlags]::NonPublic)
$LogEntries.GetValue($null).Count
[Sqlcollaborative.Dbatools.Message.LogHost]::OutQueueError.Count
[Sqlcollaborative.Dbatools.Message.LogHost]::OutQueueLog.Count
Invoke-DbaQuery -Database 'IronmanSoftware' -Query 'SELECT * FROM [IronmanSoftware].[dbo].[Orders]' -SqlInstance '(localdb)\MSSQLLocalDB' | Out-Null
The output looks like this. You can use this to see if your log queues are filling up.
Apr 28, 2022 3:00 PM 0
Apr 28, 2022 3:00 PM 1024
Apr 28, 2022 3:00 PM 0
Apr 28, 2022 3:00 PM 21
You can disable dbatools logging like this.
Set-DbatoolsConfig -FullName logging.errorlogenabled -Value $false | Register-DbatoolsConfig
Set-DbatoolsConfig -FullName logging.errorlogfileenabled -Value $false | Register-DbatoolsConfig
Set-DbatoolsConfig -FullName logging.messagelogenabled -Value $false | Register-DbatoolsConfig
Set-DbatoolsConfig -FullName logging.messagelogfileenabled -Value $false | Register-DbatoolsConfig
You can also remove all log messages as well by clearing the log queue.
$LogEntries = [Sqlcollaborative.Dbatools.Message.LogHost].GetField("LogEntries", [System.Reflection.BindingFlags]::Static -bor [System.Reflection.BindingFlags]::NonPublic)
$LogEntries.GetValue($null).Clear()
After adding that, my memory usage is much lower.