psutil
moduleWe can retrieve information about swap memory using the psutil
module in Python.
psutil
(Python system and process utilities) is a Python package that retrieves information about ongoing processes and system usage (CPU, memory, storage, network, and sensors). It is mostly used for system monitoring, profiling, restricting process resources, and process management.
The module can be installed via pip
as follows:
pip install psutil
swap_memory
methodThe swap_memory
returns the swap memory statistics.
psutil.swap_memory()
The method has no parameters.
The method returns a named tuple with the following attributes:
total
: This is the total swap memory.used
: This is the used swap memory.free
: This is the free swap memory.percent
: This is the percentage usage calculated as
(total - available) / total * 100
.sin
: This is the number of bytes the system has swapped in from the disk.sout
: This is the number of bytes the system has swapped out from the disk.All the above fields are in bytes.
import psutilprint("Swap Memory Information:")print(psutil.swap_memory())
psutil
module.swap_memory()
method.