Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory calculation #40

Merged
merged 3 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 13 additions & 22 deletions addon/globalPlugins/resourceMonitor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import winVersion
import psutil

from . import memory

# Windows Server systems prior to Server 2025 do not include wlanapi.dll.
try:
from . import wlanapi
Expand Down Expand Up @@ -428,31 +430,20 @@ def script_announceProcessorInfo(self, gesture):
speakOnDemand=True,
)
def script_announceRamInfo(self, gesture):
ram = psutil.virtual_memory()
physicalRamUsed, physicalRamTotal = memory.get_physical_memory()
# Translators: Shows RAM (physical memory) usage.
info = _("Physical: {physicalUsed} of {physicalTotal} used ({physicalPercent}%). ").format(
physicalUsed=size(ram[3], alternative),
physicalTotal=size(ram[0], alternative),
physicalPercent=tryTrunk(ram[2]),
physicalUsed=size(physicalRamUsed, alternative),
physicalTotal=size(physicalRamTotal, alternative),
physicalPercent=tryTrunk(round(physicalRamUsed / physicalRamTotal * 100, 1))
)
virtualRamUsed, virtualRamTotal = memory.get_virtual_memory()
# Translators: Shows virtual memory usage.
info += _("Virtual: {virtualUsed} of {virtualTotal} used ({virtualPercent}%).").format(
virtualUsed=size(virtualRamUsed, alternative),
virtualTotal=size(virtualRamTotal, alternative),
virtualPercent=tryTrunk(round(virtualRamUsed / virtualRamTotal * 100, 1))
)
# psutil 5.9.0 returns size of the swap file when swap_memory function is called.
# Therefore, combine swap file and RAM capacities for backward compatibility.
# psutil 5.9.5 causes virtual memory to not be reported on some systems
# due to disabled performance counters reported by an API function.
try:
virtualRam = list(psutil.swap_memory())
virtualRam[1] += ram[3]
virtualRam[0] += ram[0]
virtualRam[3] = round((virtualRam[1] / virtualRam[0]) * 100, 1)
# Translators: Shows virtual memory usage.
info += _("Virtual: {virtualUsed} of {virtualTotal} used ({virtualPercent}%).").format(
virtualUsed=size(virtualRam[1], alternative),
virtualTotal=size(virtualRam[0], alternative),
virtualPercent=tryTrunk(virtualRam[3]),
)
except RuntimeError:
# Translators: Reported when virtual memory information cannot be obtained.
info += _("Virtual memory information unavailable")
if scriptHandler.getLastScriptRepeatCount() == 0:
ui.message(info)
else:
Expand Down
11 changes: 11 additions & 0 deletions addon/globalPlugins/resourceMonitor/memory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from . import psutil


def get_physical_memory():
memory = psutil.virtual_memory()
return memory.used, memory.total


def get_virtual_memory():
memory = psutil._psutil_windows.virtual_mem()
return memory[2]-memory[3], memory[2]