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.
I don't believe there is any parallel to be found in .NET, and therefore none in PowerShell (which really just overlays .NET).
While not confirmed by the class documentation, the second paragraph in the following article does state "performance information such as the amount of memory the process is using":
My understanding of the [System.Process] class is that it pulls the memory statistics from the various Process\Memory counters (which all end in "64", i.e. WorkingSet64) and it therefore subservient to the Windows performance management framework.
The .NET results correlate those produced by SysInternals' pslist64.exe (with the exclusion of private working set, which is excluded from the .NET class for some reason).
I'm definitely not privy to how the counters themselves are maintained, but anecdotally, neither .NET nor pslist.exe seem to use the WinAPI function you mentioned given the results don't match.
If that is indeed the case then all you can do is write your own function in PowerShell that actions a P/Invoke call to that function after the necessary types have been defined and imported.
P/Invoking is an ugly business and not something I look to leverage, but if you can handle the pain, you can get the outcome you're seeking.
- Use PowerShell to Duplicate Process Tokens via P/Invoke - Scripting Blog [archived] (microsoft.com)
- PINVOKE or accessing WIN32 APIs - PowerShell Team (microsoft.com)
Cheers,
Lain