Can one get the RAM Utilization with python and/or Powershell, without Cache?

121 views Asked by At

Is there a way to get the RAM Utilization with powershell/Python, without the cached ram? We want to monitor the utilization of our System parameters. At the moment I use this in powershell, which gets calles by task scheduler every 30 minutes together with other parameter checks:

...
$counts = 6
$Samples = 145
for($j=0; $j -lt $counts; $j++){
    $temp = 0
    $result = 0
    for ($i=0; $i -lt $Samples; $i++){
        $os = Get-WMIObject Win32_OperatingSystem
        $os | select *memory*
        $pctUsed = [math]::Round(100-($os.FreePhysicalMemory/$os.TotalVisibleMemorySize)*100,2)
        $temp = $pctUsed + $temp
        Start-Sleep -s 2
    }
$result = $temp/$Samples
...

which will then be sent to a Monitoring application of us.

How can we get RAM without Cache? Is there a way?

1

There are 1 answers

0
Victor Goanta On

Try this, it checks the Ram usage in real time.

Function Check_RAM{
$time= Get-Date
$usedMemory = Get-CimInstance -Class Win32_OperatingSystem
$totalMemory = $usedMemory.TotalVisibleMemorySize
$freeMemory = $usedMemory.FreePhysicalMemory
$usedMemoryPercentage = ($totalMemory - $freeMemory) / $totalMemory * 100
Write-Host "Used Memory Percentage $usedMemoryPercentage% at $time"
}
do{
Check_RAM
start-sleep -Seconds 10
}until($infinity)