Forum Discussion
Dec 06, 2023
Need help determine Total Allocated Virtual Memory
I'm trying to determine the Total Allocate Virtual Memory of a Process but can't seem to find any examples.
I know I can do
Get-Process -Name 'MyProcessName' | Format-Table ` @{Label = "VM(M)"; Expression = {[int]($_.VM / 1MB)}}
to retrieve the currently used virtual memory, but I'd like to also retrieve the total available memory for the same process.
I know MemoryStatusEx should provide such information, but not sure how I can use it in PS or if I'm simply missing some built-in cmdlet that can do this?
Ideally, I'd love to be able to list a process of series of process and list (VM - virtual memory)
Process Name | Total VM | Used VM | Remaining VM
4 Replies
Sort By
- LeonPavesicSilver Contributor
Hi Daniel_Pineault,
you can try to use the following PowerShell script to export all processes with Process Name, Total VM, Used VM and Remaining VM:$processes = Get-Process foreach ($process in $processes) { $totalVM = $process.VirtualMemorySize64 / 1MB $usedVM = $process.WorkingSet64 / 1MB $remainingVM = $totalVM - $usedVM $process | Format-Table @{Label = "Process Name"; Expression = {$_.Name}}, @{Label = "Total VM(MB)"; Expression = {$totalVM}}, @{Label = "Used VM(MB)"; Expression = {$usedVM}}, @{Label = "Remaining VM(MB)"; Expression = {$remainingVM}} }
If you like it better, you can use the same script, but to export all the data in CSV file:$processes = Get-Process $output = foreach ($process in $processes) { $totalVM = $process.VirtualMemorySize64 / 1MB $usedVM = $process.WorkingSet64 / 1MB $remainingVM = $totalVM - $usedVM [PSCustomObject]@{ "Process Name" = $process.Name "Total VM(MB)" = $totalVM "Used VM(MB)" = $usedVM "Remaining VM(MB)" = $remainingVM } } $output | Export-Csv -Path "ProcessInfo.csv" -NoTypeInformation
Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.If the post was useful in other ways, please consider giving it Like.
Kindest regards,
Leon Pavesic
(LinkedIn)- This doesn't match up with what I get using MemoryStatusEx Api.
With this script I'm getting
Process Name Total VM(MB) Used VM(MB) Remaining VM(MB)
------------ ------------ ----------- ----------------
EXCEL 617.43359375 104.98046875 512.453125
Yet via MemoryStatusEx I get
Memory available: ( 2047 MB) - allocated: (594 MB) - free: ( 1453 MB)
The Memory available of 2GB makes sense for a 32-bit app, so I have tendency to believe the API, but wish to do this via PS. Do you know why the numbers don't match up?- LeonPavesicSilver Contributor
Hi Daniel_Pineault,
thanks for your update.The difference in numbers between the PowerShell script using the Get-Process cmdlet and the MemoryStatusEx API results might be due to the different ways they calculate and report memory information.
The Get-Process cmdlet retrieves information from the Windows performance counters, which may not align directly with the information obtained from the MemoryStatusEx API, as it queries the system memory status directly.
The MemoryStatusEx API provides information about the overall system memory, including available, allocated, and free memory. Conversely, the Get-Process cmdlet focuses on the memory usage of a specific process, reporting metrics such as Virtual Memory Size (VirtualMemorySize64) and Working Set (WorkingSet64).
If you want to retrieve memory information similar to what the MemoryStatusEx API provides, you might need to explore other Windows API functions or use P/Invoke in PowerShell to call native functions. This approach requires more advanced scripting and knowledge of Windows API programming.
Here are a few suggestions:
P/Invoke in PowerShell: If you decide to explore P/Invoke, you can refer to the following resources:
- Use PowerShell to Duplicate Process Tokens via P/Invoke
- PINVOKE or accessing WIN32 APIs - PowerShell Team
Windows API Functions: Consider Windows API functions like GlobalMemoryStatusEx, which can be used to retrieve system-wide memory information.
Performance Counters: Explore other performance counters related to system memory, which you can access using PowerShell’s Get-Counter cmdlet.
Here’s a basic example using Get-Counter:
$memoryCounter = Get-Counter '\Memory\Available MBytes' $availableMemory = $memoryCounter.CounterSamples.CookedValue Write-Host "Available Memory: $availableMemory MB"
Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.If the post was useful in other ways, please consider giving it Like.
Kindest regards,
Leon Pavesic
(LinkedIn)