LinuxMemoryStatistics (version 0.0.1)
index
linuxmemorystatistics.py

This script prints memory usages and statistics (by process,
executable and for the full system).

 
Classes
       
builtins.object
ExecutableStatistics
builtins.tuple(builtins.object)
MemoryStatistics

 
class ExecutableStatistics(builtins.object)
    ExecutableStatistics(executable: str, number_of_process: int, private: int, shared: int, total: int, pss: int, pfn_size: int, missing_pfn_size: int) -> None
 
ExecutableStatistics(executable: str, number_of_process: int, private: int, shared: int, total: int, pss: int, pfn_size: int, missing_pfn_size: int)
 
  Methods defined here:
__eq__(self, other)
Return self==value.
__init__(self, executable: str, number_of_process: int, private: int, shared: int, total: int, pss: int, pfn_size: int, missing_pfn_size: int) -> None
Initialize self.  See help(type(self)) for accurate signature.
__repr__(self)
Return repr(self).

Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)

Data and other attributes defined here:
__annotations__ = {'executable': <class 'str'>, 'missing_pfn_size': <class 'int'>, 'number_of_process': <class 'int'>, 'pfn_size': <class 'int'>, 'private': <class 'int'>, 'pss': <class 'int'>, 'shared': <class 'int'>, 'total': <class 'int'>}
__dataclass_fields__ = {'executable': Field(name='executable',type=<class 'str'>,defau...appingproxy({}),kw_only=False,_field_type=_FIELD), 'missing_pfn_size': Field(name='missing_pfn_size',type=<class 'int'>...appingproxy({}),kw_only=False,_field_type=_FIELD), 'number_of_process': Field(name='number_of_process',type=<class 'int'...appingproxy({}),kw_only=False,_field_type=_FIELD), 'pfn_size': Field(name='pfn_size',type=<class 'int'>,default...appingproxy({}),kw_only=False,_field_type=_FIELD), 'private': Field(name='private',type=<class 'int'>,default=...appingproxy({}),kw_only=False,_field_type=_FIELD), 'pss': Field(name='pss',type=<class 'int'>,default=<dat...appingproxy({}),kw_only=False,_field_type=_FIELD), 'shared': Field(name='shared',type=<class 'int'>,default=<...appingproxy({}),kw_only=False,_field_type=_FIELD), 'total': Field(name='total',type=<class 'int'>,default=<d...appingproxy({}),kw_only=False,_field_type=_FIELD)}
__dataclass_params__ = _DataclassParams(init=True,repr=True,eq=True,order=False,unsafe_hash=False,frozen=False)
__hash__ = None
__match_args__ = ('executable', 'number_of_process', 'private', 'shared', 'total', 'pss', 'pfn_size', 'missing_pfn_size')

 
class MemoryStatistics(builtins.tuple)
    MemoryStatistics(pid, private, shared, rss, pss, executable, cmd)
 
MemoryStatistics(pid, private, shared, rss, pss, executable, cmd)
 
 
Method resolution order:
MemoryStatistics
builtins.tuple
builtins.object

Methods defined here:
__getnewargs__(self)
Return self as a plain tuple.  Used by copy and pickle.
__repr__(self)
Return a nicely formatted representation string
_asdict(self)
Return a new dict which maps field names to their values.
_replace(self, /, **kwds)
Return a new MemoryStatistics object replacing specified fields with new values

Class methods defined here:
_make(iterable) from builtins.type
Make a new MemoryStatistics object from a sequence or iterable

Static methods defined here:
__new__(_cls, pid, private, shared, rss, pss, executable, cmd)
Create new instance of MemoryStatistics(pid, private, shared, rss, pss, executable, cmd)

Data descriptors defined here:
pid
Alias for field number 0
private
Alias for field number 1
shared
Alias for field number 2
rss
Alias for field number 3
pss
Alias for field number 4
executable
Alias for field number 5
cmd
Alias for field number 6

Data and other attributes defined here:
__match_args__ = ('pid', 'private', 'shared', 'rss', 'pss', 'executable', 'cmd')
_field_defaults = {}
_fields = ('pid', 'private', 'shared', 'rss', 'pss', 'executable', 'cmd')

Methods inherited from builtins.tuple:
__add__(self, value, /)
Return self+value.
__contains__(self, key, /)
Return key in self.
__eq__(self, value, /)
Return self==value.
__ge__(self, value, /)
Return self>=value.
__getattribute__(self, name, /)
Return getattr(self, name).
__getitem__(self, key, /)
Return self[key].
__gt__(self, value, /)
Return self>value.
__hash__(self, /)
Return hash(self).
__iter__(self, /)
Implement iter(self).
__le__(self, value, /)
Return self<=value.
__len__(self, /)
Return len(self).
__lt__(self, value, /)
Return self<value.
__mul__(self, value, /)
Return self*value.
__ne__(self, value, /)
Return self!=value.
__rmul__(self, value, /)
Return value*self.
count(self, value, /)
Return number of occurrences of value.
index(self, value, start=0, stop=9223372036854775807, /)
Return first index of value.
 
Raises ValueError if the value is not present.

Class methods inherited from builtins.tuple:
__class_getitem__(...) from builtins.type
See PEP 585

 
Functions
       
calculate_total_memory_kib(pfns: Set[int]) -> int
Return total memory size in KiB from PFN count
exit(status=None, /)
Exit the interpreter by raising SystemExit(status).
 
If the status is omitted or None, it defaults to zero (i.e., success).
If the status is an integer, it will be used as the system exit status.
If it is another kind of object, it will be printed and the system
exit status will be one (i.e., failure).
get_all_pfns(executables_statistics: List[LinuxMemoryStatistics.MemoryStatistics]) -> Tuple[Set[int], int]
Extract unique PFNs from a list of PIDs
 
Return:
    - set of present PFNs
    - total size in KiB of pages with no PFN (i.e. missing from RAM)
get_cmdline(pid: str) -> List[str]
This function returns the process command line arguments.
get_memory_statistics() -> Tuple[List[LinuxMemoryStatistics.MemoryStatistics], List[str]]
This function returns memory statistics from smaps and PID with errors.
get_pfn(pid: int, vaddr: int) -> int
Return PFN from /proc/[pid]/pagemap for a virtual address
get_process(pid: str) -> str
This function returns the process command line arguments.
get_size_format(kb: int) -> str
This function returns the string the represent size.
get_statistics_by_executable(statistics: List[LinuxMemoryStatistics.MemoryStatistics], calcul_pfns: bool) -> Dict[str, LinuxMemoryStatistics.ExecutableStatistics]
This function groups statistics by executable.
kb_to_gib(kb: int) -> float
This function converts KB in GB.
kb_to_mib(kb: int) -> float
This function converts KB in MB.
listdir(path=None)
Return a list containing the names of the files in the directory.
 
path can be specified as either str, bytes, or a path-like object.  If path is bytes,
  the filenames returned will also be bytes; in all other circumstances
  the filenames returned will be str.
If path is None, uses the path='.'.
On some platforms, path may also be specified as an open file descriptor;\
  the file descriptor must refer to a directory.
  If this functionality is unavailable, using it raises NotImplementedError.
 
The list is in arbitrary order.  It does not include the special
entries '.' and '..' even if they are present in the directory.
main() -> int
The main function to start the script and print outputs/errors.
parse_args() -> argparse.Namespace
This function parses the command line arugments.
parse_maps(pid: int) -> Iterator[Tuple[int, int]]
Yield (start, end) virtual address ranges for a PID
parse_meminfo() -> Dict[str, int]
This function returns the system memory usages.
parse_smaps(pid: str) -> Tuple[int, int, int, int]
This function returns private memory, shared memory,
rss (total memory used by process) and pss (memory cost by process)
for a process.
print_colored_size_value(*to_print: str, value: int = 0, total: int = 0, color: bool = True, **kwargs) -> None
This function is a special print to manage color.
print_details(statistics: List[LinuxMemoryStatistics.MemoryStatistics], meminfo: Dict[str, int], color: bool = True, order: str = 'pss') -> None
This function prints memory usages by process.
print_executables(statistics: Dict[str, LinuxMemoryStatistics.ExecutableStatistics], meminfo: Dict[str, int], color: bool = True, order: str = 'pss') -> None
This function prints memory usages by executable.
print_system_memory_usage(meminfo: Dict[str, int], color: bool = True) -> None
This function prints the system memory usage.
sysconf lambda x

 
Data
        Dict = typing.Dict
Iterator = typing.Iterator
List = typing.List
PAGE_SIZE = 4096
Set = typing.Set
Tuple = typing.Tuple
__author_email__ = 'mauricelambert434@gmail.com'
__copyright__ = '\nLinuxMemoryStatistics Copyright (C) 2025 Maur...ome to redistribute it\nunder certain conditions.\n'
__description__ = '\nThis script prints memory usages and statistics (by process,\nexecutable and for the full system).\n'
__license__ = 'GPL-3.0 License'
__maintainer__ = 'Maurice Lambert'
__maintainer_email__ = 'mauricelambert434@gmail.com'
__url__ = 'https://github.com/mauricelambert/LinuxMemoryStatistics'
copyright = '\nLinuxMemoryStatistics Copyright (C) 2025 Maur...ome to redistribute it\nunder certain conditions.\n'
fields = ['pid', 'private', 'shared', 'rss', 'pss', 'executable', 'cmd']
license = 'GPL-3.0 License'
stderr = <_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'>

 
Author
        Maurice Lambert