11

How do I find out (in Powershell) what process/whatever uses the most memory?

Edit: I am trying to figure out how to use Powershell to find out what's using all the physical memory in case Task Manager etc. fail to explain why all the physical RAM is used up. I.e. I need to identify memory used by caches etc.

Andrew J. Brehm
  • 1,611
  • 7
  • 37
  • 57

3 Answers3

10

Here's a way to get info on currently running processes and sort by Working Set size

Get-Process | Sort-Object -Descending WS

Assign that output to a variable and it'll give you an array of the results, then you can just write out the first member of the array (which in this case will be a System.Diagnostics.Process object).

$ProcessList = Get-Process | Sort-Object -Descending WS
Write-Host $ProcessList[0].Handle "::" $Process.ProcessName "::" $Process.WorkingSet

Here's another quick and dirty script to dump a few items of data from the list of currently running processes using WMI's Win32_Process provider:

$ProcessList = Get-WmiObject Win32_Process -ComputerName mycomputername
foreach ($Process in $ProcessList) {
    write-host $Process.Handle "::" $Process.Name "::" $Process.WorkingSetSize
}

That'll list the PID (handle), process name and the current working set size. You can change that up using different properties of the WMI Process class.

squillman
  • 37,883
  • 12
  • 92
  • 146
2

One liner to find the name of your highest memory usage process

Get-Process | Sort-Object -Descending WS | select -first 1 | select -ExpandProperty ProcessName
Eric
  • 554
  • 1
  • 5
  • 15
0
$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;
Jason
  • 3,931
  • 19
  • 66
  • 107
Suresh
  • 11