Monday, July 29, 2013

Current Process Memory

updated  08/04/2013 -RMF

Below is an example of the (clumsy) scripting workaround I will use because I always have a difficult time getting 'add-member' to work with membertype ScriptMethod. What I want here is WS, PM, VM for each current process, but also the differences (e.g. VM - PM, PM - WS):

Function CurrentMem {
[PSObject]$Memory=
ps * | Select Name, ID,`
@{Name='WS_MB';Expression={[INT](($_.WorkingSet)/1MB)}},`
@{Name='PriMemMB';Expression={[INT](($_.PrivateMemorySize64)/1MB)}}, `
@{Name='VirMemMB';Expression={[INT](($_.VirtualMemorySize64)/1MB)}}
$Memory | Sort -desc VirMemMB | 
Select *,  @{Name='VM-PM';Expression={[INT]($_.VirMemMB - $_.PriMemMB)}}, `
@{Name='PM-WS';Expression={[INT]($_.PriMemMB - $_.WS_MB)}}
}

So then  I have:

PS C:\> $CurrentMem=CurrentMem
PS C:\> ($CurrentMem | sort -desc VM-PM)[0..10]  | ft -auto

Name         Id WS_MB PriMemMB VirMemMB VM-PM PM-WS
----         -- ----- -------- -------- ----- -----
powershell 7300    74       66      608   542    -8
powershell 5408     3       63      603   540    60
svchost     536    39       39      575   536     0
chrome     4748   192      194      658   464     2
bash       6348     0        8      451   443     8
powershell 1536   176      185      620   435     9
syslogd    2112     1        5      432   427     4
cygrunsrv  1948     0        6      427   421     6
chrome     2956   265      323      742   419    58
taskhost   3896     6       15      394   379     9
svchost    1288    23       26      386   360     3

A more advanced version allows for a more granular look at processes:


Function Get-CM {
[PSObject]$Memory=
ps * | Select Name, ID, Company, HandleCount,`
@{Name='WorkSetMB';Expression={[LONG](($_.WorkingSet)/1MB)}},`
@{Name='PMS64Bytes';Expression={[LONG](($_.PagedMemorySize64))}},`
@{Name='NPMS64Bytes';Expression={[LONG](($_.NonpagedSystemMemorySize64))}},`
@{Name='PriMem64MB';Expression={[LONG](($_.PrivateMemorySize64)/1MB)}},`
@{Name='VirMem64MB';Expression={[LONG](($_.VirtualMemorySize64)/1MB)}}
$Memory | Sort -desc VirMem64MB | 
Select *,  @{Name='VMem-PMem.MB';Expression={[LONG]($_.VirMem64MB - $_.PriMem64MB)}},`
@{Name='PMem-NPMem.MB';Expression={[LONG](($_.PMS64Bytes - $_.NPMS64Bytes)/1MB)}},`
@{Name='PriMem-WorkSet.MB';Expression={[LONG]($_.PriMem64MB - $_.WorkSetMB)}}
}

So that here I can look at all process with over 500 handles sorted by a decreasing amount of PagedMemorySystem64 :

PS C:\ps1> get-cm | where HandleCount -gt 500 | sort -desc PMS64Bytes | fl *


Name              : chrome
Id                : 6636
Company           : The Chromium Authors
HandleCount       : 2769
WorkSetMB         : 180
PMS64Bytes        : 268677120
NPMS64Bytes       : 141712
PriMem64MB        : 256
VirMem64MB        : 672
VMem-PMem.MB      : 416
PMem-NPMem.MB     : 256
PriMem-WorkSet.MB : 76

Name              : chrome
Id                : 3532
Company           : The Chromium Authors
HandleCount       : 796
WorkSetMB         : 63
PMS64Bytes        : 220286976
NPMS64Bytes       : 69632
PriMem64MB        : 210
VirMem64MB        : 699
VMem-PMem.MB      : 489
PMem-NPMem.MB     : 210
PriMem-WorkSet.MB : 147

Name              : svchost
Id                : 456
Company           : Microsoft Corporation
HandleCount       : 727
WorkSetMB         : 137
PMS64Bytes        : 162627584
NPMS64Bytes       : 31784
PriMem64MB        : 155
VirMem64MB        : 315
VMem-PMem.MB      : 160
PMem-NPMem.MB     : 155
PriMem-WorkSet.MB : 18

...

or (click to enlarge)