Best way to troubleshoot high memory and CPU usage? (and crashes)

I made this “auto runner” :slight_smile:

Start-Transcript $psscriptroot\logs\autorun.txt -Append -Force
$InformationPreference = ‘Continue’
while ($true) {
if ($Dashboard.HasExited) {
Write-Information -MessageData “[$(Get-Date -Format ‘dd/MM/yy HH:mm:ss’)] Dashboard Crashed”
$start = $true
}
if ($Start -ne $false) {
Write-Information “[$(Get-Date -Format ‘dd/MM/yy HH:mm:ss’)] Starting Dashboard”
$Dashboard = Start-Process -PassThru -WorkingDirectory $psscriptroot -FilePath pwsh.exe -WindowStyle Minimized -ArgumentList “-file $psscriptroot\invoke-dashboard.ps1”
$start = $false
}
$MemoryUsed = (Get-Process -Id $Dashboard.Id).WorkingSet64 / 1mb
if ($MemoryUsed -gt 1024) {
Write-Information “[$(Get-Date -Format ‘dd/MM/yy HH:mm:ss’)] Dashboard reached memory limit, restarting dashboard”
$Dashboard.Kill()
$Dashboard = $null
$start = $true
}

Need to Fetch before Status to compare against server status

git fetch
$status = git status
if($status -like “Your Branch is behind”) {
Write-Information “[$(Get-Date -Format ‘dd/MM/yy HH:mm:ss’)] Dashboard Updating, restarting dashboard”
$Dashboard.Kill()
$Dashboard = $null
$start = $true
git pull
}
Start-Sleep -Seconds 60
}

also I removed the usage of dbatools module, as that were the one that took most of the memory usage, so now the dashboard can run for 12 hours ish, before it reached 1gb of ram memory.
much better than before.
for the SQL part, I use netstandard 2.0 SMO library: https://github.com/Microsoft/sqltoolsservice