Forum Discussion
Need help determine Total Allocated Virtual Memory
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?
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)