Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
    
What kind of cache are you thinking of? – squillman Jan 18 '10 at 17:32
    
Disk cache... Wouldn't Windows usually try to use all available physical memory for something useful? – Andrew J. Brehm Jan 18 '10 at 19:36
up vote 6 down vote accepted

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.

share|improve this answer
    
My bad. I wasn't clear enough. Question edited... – Andrew J. Brehm Jan 18 '10 at 17:24
    
It's not getting better than this. Thanks. – Andrew J. Brehm Feb 15 '10 at 15:53

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

Get-Process | Sort-Object -Descending WS | select -first 1 | select -ExpandProperty ProcessName
share|improve this answer
$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;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.