Skip to content

Commit

Permalink
[Windows] psutil.swap_memory() show swap instead of committed memory
Browse files Browse the repository at this point in the history
Signed-off-by: David Knaack <davidkna@users.noreply.github.com>
  • Loading branch information
davidkna committed Mar 14, 2021
1 parent c3e63b4 commit 2bf3e7c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -740,3 +740,7 @@ I: 1851
N: guille
W: https://github.com/guille
I: 1913

N: David Knaack
W: https://github.com/davidkna
I: 1921
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ XXXX-XX-XX
called after sprintf(). (patch by alxchk)
- 1874_: [Solaris] swap output error due to incorrect range.
- 1913_: [Linux] wait_procs seemingly ignoring timeout, TimeoutExpired thrown
- 1921_: [Windows] psutil.swap_memory() shows committed memory instead of swap

5.8.0
=====
Expand Down
17 changes: 15 additions & 2 deletions psutil/_pswindows.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,21 @@ def virtual_memory():
def swap_memory():
"""Swap system memory as a (total, used, free, sin, sout) tuple."""
mem = cext.virtual_mem()
total = mem[2]
free = mem[3]

total_phys = mem[0]
free_phys = mem[1]
total_system = mem[2]
free_system = mem[3]

# Despite the name PageFile refers to total system memory here
# thus physical memory values need to be substracted to get swap values
total = total_system - total_phys
if total <= 0:
total = 0
free = free_system - free_phys
if free <= 0:
free = 0

used = total - free
percent = usage_percent(used, total, round_=1)
return _common.sswap(total, used, free, percent, 0, 0)
Expand Down

0 comments on commit 2bf3e7c

Please sign in to comment.