powershellmemory-consumption

PowerShell - how can I get memory consumption peak for my exe program from PowerShell


I have a program which allocates memory. I am running it from Windows PowerShell command line. It can run for 1-2 hours allocating and releasing memory blocks.

What I am looking for is a way to get at the end (when the program finishes) some memory consumption statistics. More specifically, what was the peak usage of the memory (max memory allocated).


Solution

  • Get-Process -Id xxx gives you the Process object instance of the process with ID xxx. There are all kinds of memory-related properties in there, including things like PeakVirtualMemorySize64 and PeakWorkingSet64. Pick the ones you find useful.

    You can even set up a background job to get a data series, something like

    $proc = Start-Process "your_long_running.exe" -PassThru
    
    $memoryWatcher = Start-Job -ScriptBlock {
        while ($true) {
            Get-Process -Id $args[0] | Select VirtualMemorySize64,PeakVirtualMemorySize64
            Start-Sleep -Seconds 1
        }
    } -ArgumentList $proc.Id
    
    # now wait for the process to end
    Wait-Process -Id $proc.Id
        
    $memoryWatcher.StopJob()
    $results = Receive-Job $memoryWatcher
    
    $results | Format-Table