
我如何(在 Powershell 中)找出哪個進程/什麼使用最多的記憶體?
編輯:我試圖弄清楚如何使用 Powershell 來找出什麼在使用所有物理內存,以防任務管理器等無法解釋為什麼所有實體 RAM 都被用完。即我需要識別快取等使用的記憶體。
答案1
這是一種獲取當前正在運行的進程的資訊並按工作集大小排序的方法
Get-Process | Sort-Object -Descending WS
將該輸出分配給一個變量,它將為您提供一個結果數組,然後您可以寫出該數組的第一個成員(在本例中將是一個系統診斷過程目的)。
$ProcessList = Get-Process | Sort-Object -Descending WS
Write-Host $ProcessList[0].Handle "::" $Process.ProcessName "::" $Process.WorkingSet
這是另一個快速但骯髒的腳本,用於使用 WMI 的 Win32_Process 提供者從目前正在執行的進程清單中轉儲一些資料項:
$ProcessList = Get-WmiObject Win32_Process -ComputerName mycomputername
foreach ($Process in $ProcessList) {
write-host $Process.Handle "::" $Process.Name "::" $Process.WorkingSetSize
}
這將列出 PID(句柄)、進程名稱和目前工作集大小。您可以使用不同的屬性來更改它WMI 進程類。
答案2
一個尋找記憶體使用率最高的進程的名稱的行
Get-Process | Sort-Object -Descending WS | select -first 1 | select -ExpandProperty ProcessName
答案3
$scripthost = Read-Host "Enter the Hostname of the Computer you would like to check Memory Statistics for"
""
""
"===========CPU - Top 10 Utilization List==========="
gwmi -computername $scripthost Win32_PerfFormattedData_PerfProc_Process| sort PercentProcessorTime -desc | select Name,PercentProcessorTime | Select -First 10 | ft -auto
"===========Memory - Top 10 Utilization List==========="
gwmi -computername $scripthost Win32_Process | Sort WorkingSetSize -Descending | Select Name,CommandLine,@{n="Private Memory(mb)";Expression = {[math]::round(($_.WorkingSetSize / 1mb), 2)}} | Select -First 10 | Out-String
#gwmi -computername $scripthost Win32_Process | Sort WorkingSetSize -Descending | Select Name,CommandLine,@{n="Private Memory(mb)";e={$_.WorkingSetSize/1mb}} | Select -First 10 | Out-String
#$fields = "Name",@{label = "Memory (MB)"; Expression = {[math]::round(($_.ws / 1mb), 2)}; Align = "Right"};
"===========Server Memory Information==========="
$fieldPercentage = @{Name = "Memory Percentage in Use (%)"; Expression = { “{0:N2}” -f ((($_.TotalVisibleMemorySize - $_.FreePhysicalMemory)*100)/ $_.TotalVisibleMemorySize)}};
$fieldfreeram = @{label = "Available Physical Memory (MB)"; Expression = {[math]::round(($_.FreePhysicalMemory / 1kb), 2)}};
$fieldtotalram = @{label = "Total Physical Memory (MB)"; Expression = {[math]::round(($_.TotalVisibleMemorySize / 1kb), 2)}};
$fieldfreeVram = @{label = "Available Virtual Memory (MB)"; Expression = {[math]::round(($_.FreeVirtualMemory / 1kb), 2)}};
$fieldtotalVram = @{label = "Total Virtual Memory (MB)"; Expression = {[math]::round(($_.TotalVirtualMemorySize /1kb), 2)}};
$memtotal = Get-WmiObject -Class win32_OperatingSystem -ComputerName $scripthost;
$memtotal | Format-List $fieldPercentage,$fieldfreeram,$fieldtotalram,$fieldfreeVram,$fieldtotalVram;