PoshCode Logo PowerShell Code Repository

app memory deltas (modification of post by view diff)
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/788"></script>download | new post

Uses get-process (ps) to measure application memory deltas over specified interval. Run ‘.\ws_diff [Interval in Seconds] [Process Name] or
without [Process Name]. To log all processes continually every 10 seconds run: ‘while (1) {.\WS_diff.ps1 10 cmd >> ps_out.txt }’

  1. ## Use ps to measure Application Memory Deltas
  2. ## Run '.\ws_diff [Interval in Seconds] [Process Name] or
  3. ## to log all processes continually every 10 seconds -- 'while (1) {.\WS_diff.ps1 10 cmd >> ps_out.txt }'
  4.  
  5. # Create args as Variables or Objects
  6.  $sleep_time = $args[0]
  7. #$sleep_time = 10
  8.  
  9. # Create or define PS_Array. Default is ps is called without args.
  10. # Then take measurements $now, $then, $count
  11. if ($args[1] -eq $NULL )
  12.     {
  13.     $then = ps | %{$_ | Select Name,ID,WorkingSet,PrivateMemorySize,VirtualMemorySize}
  14.     sleep -seconds $sleep_time
  15.     $now  = ps | %{$_ | Select Name,ID,WorkingSet,PrivateMemorySize,VirtualMemorySize}
  16.     $count = ($now | Select Name).count
  17.     }
  18. else
  19.     {
  20.     $ps_array = ( ps $args[1] )
  21.     $then = ps -inputobject $ps_array | %{$_ | Select Name,ID,WorkingSet,PrivateMemorySize,VirtualMemorySize}
  22.     sleep -seconds $sleep_time
  23.     $now  = ps -inputobject $ps_array | %{$_ | Select Name,ID,WorkingSet,PrivateMemorySize,VirtualMemorySize}
  24.     $count = ($now | Select Name).count
  25.     }
  26.  
  27. # Declare Sample Time Measurement
  28. # Declare Hours.Minutes.Seconds.Milliseconds in body of loop for more accuracy
  29. $date = (get-date -format g)
  30.  
  31. # Write output and find diffs. Check if process has multiple instances first
  32. if ( $count -gt 1 )
  33. {
  34.     $array_out = 0..$count |
  35.     %{
  36.     $date + "," +
  37.     [DateTime]::UtcNow.TimeOfDay.Hours + ":" +
  38.     [DateTime]::UtcNow.TimeOfDay.Minutes + ":" +
  39.     [DateTime]::UtcNow.TimeOfDay.Seconds + ":"  +
  40.     [DateTime]::UtcNow.TimeOfDay.Milliseconds + "," +
  41.     $sleep_time + "," + $now[$_].Name + "," + $now[$_].ID + "," +
  42.     $now[$_].WorkingSet + "," +
  43.     $now[$_].PrivateMemorySize + "," +
  44.     $now[$_].VirtualMemorySize + "," +
  45.     ( ($now[$_].WorkingSet) - ($then[$_].WorkingSet) ) + "," +
  46.     ( ($now[$_].PrivateMemorySize) - ($then[$_].PrivateMemorySize) ) + "," +
  47.     ( ($now[$_].VirtualMemorySize) - ($then[$_].VirtualMemorySize) )
  48.     }
  49. }
  50.  
  51. else  
  52. {
  53.     $array_out =
  54.     $date + "," +
  55.     [DateTime]::UtcNow.TimeOfDay.Hours + ":" +
  56.     [DateTime]::UtcNow.TimeOfDay.Minutes + ":" +
  57.     [DateTime]::UtcNow.TimeOfDay.Seconds + ":"  +
  58.     [DateTime]::UtcNow.TimeOfDay.Milliseconds + "," +
  59.     $sleep_time + "," + $now.Name + "," + $now.ID + "," +
  60.     $now.WorkingSet + "," +
  61.     $now.PrivateMemorySize + "," +
  62.     $now.VirtualMemorySize + "," +
  63.     ( ($now.WorkingSet) - ($then.WorkingSet) ) + "," +
  64.     ( ($now.PrivateMemorySize) - ($then.PrivateMemorySize) ) + "," +
  65.     ( ($now.VirtualMemorySize) - ($then.VirtualMemorySize) )
  66. }
  67.  
  68. write $array_out

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me