Reputation: 678
I make an OS call in PowerShell to obtain the working set for a particular process.
When the working set is < 2GB, the value is returned correctly.
When the working set is between 2GB and 4GB, the value is returned as a negative number. I can xor it with 4294967295, multiply by -1, and get the correct value.
All was good in the world... until the working set exceeded 4GB. Now the value returned seems to be counting up from 4GB... IOW, I need to add the result to 4294967296.
My bitwise operational chops are a bit rusty, and I am unsure if there's a means to accurately calculate the correct value based on what the OS is returning. Ideally I'd like to have one expression that does the job, or at least a series of "if" statements.
Here's the PS code in case it clarifies the situation. it only accurately calculates the working set for values < 4GB.
$ServerName = "MyServer"
# get a collection containing all process objects on the system
$Procs = (Get-Process -ComputerName $ServerName) | Sort-Object -Descending WS
# Output each process and its working set
foreach($Proc in $Procs)
{
$WorkingSet = [int64]($Proc.WorkingSet64)
#for values greater than 2^32, a negative value will be returned... need to convert to 64 bit:
if($WorkingSet -lt 0) {
$WorkingSet = 4294967295 -bxor $WorkingSet * -1
}
$Process = $Proc.ProcessName
Write-Host "Process: $process"
Write-Host "Working Set: $WorkingSet"
Write-Host "------------------------------"
}
Upvotes: 4
Views: 842
Reputation: 11
Use WorkingSet64 instead of WS
For example:
Get-Process -Name notepad | Select-Object -ExpandProperty WorkingSet64
/André
Upvotes: 1
Reputation: 19221
It is a bug in the .NET Framework. It is only grabbing the bottom 32-bits of the result and casting it to a 64-bit number.
The problem is you don't have enough information to determine the real value once the top bits have been truncated. Do you have any range that the value could be in for certain? For instance if you knew it would be between 2 GB and 6 GB, it would be easy to map the values returned by the method.
The only general solution I can think of is to look at the other properties available and see if any of them can give you enough of a hint to determine the range of the value returned.
Upvotes: 1