Converting large integers to significant values ​​through bitwise operations

I am making an OS call in PowerShell to get a working set for a specific process.

When the working set is <2 GB, the value is returned correctly.

When the working set is between 2 and 4 GB, the value is returned as a negative number. I can use it with 4294967295, multiply by -1 and get the correct value.

Everything was fine in the world ... until the working set exceeded 4 GB. Now the return value seems to be counting from 4 GB ... IOW, I need to add the result to 4294967296.

My bitwise operational chops are a bit rusty, and I'm not sure if there is a way to accurately calculate the correct value depending on what the OS returns. Ideally, I would like to have one expression that does this work, or at least a series of "if" statements.

Here's the PS code if it clarifies the situation. it only accurately calculates the working set for values ​​<4 GB.

$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 "------------------------------" } 
+4
source share
2 answers

This is a bug in the .NET Framework . It only captures the bottom 32 bits of the result and discards it by a 64-bit number.

The problem is that you do not have enough information to determine the real value after the upper bits have been truncated. Do you have a range that can be defined for a specific? For example, if you knew that it would be from 2 GB to 6 GB, it would be easy to match the values ​​returned by the method.

The only general solution I can think of is to look at the other available properties and see if any of them can give you enough clues to determine the range of the return value.

+1
source

Use WorkSet64 instead of WS

For instance:

 Get-Process -Name notepad | Select-Object -ExpandProperty WorkingSet64 

/ André

+1
source

All Articles