Package: pmemstat
Quick Start: from the CLIIfpython3 -Vshows v3.11 or later, install withpipx:installpipxper your distro's guidancethen:pipx upgrade pmemstat || pipx install pmemstatElse for python3.10 and lesser versions, install withpip:installpipper your distro's guidancethen:python3 -m pip install --user --upgrade pmemstatTo run:simply run:pmemstattype "?" withinpmemstatto show help screen.pmemstat - Proportional Memory Statuspmemstatshows detailedproportionalmemory use ofLinuxprocesses by digesting:/proc/{PID}/smaps/proc/{PID}/smaps_rollupComputing proportional memory avoids overstating memory use as many programs do (e.g.,top). Specifically, proportional memory splits the cost of common memory to the processes sharing it (rather than counting common memory multiple times). And, it does not include uninstantiated virtual memory.Without-o,pmemstatshows less details and is much faster and sometimes less accurate (e.g., it will not report classes of memory such as SysV shared memory which are often are not present anyhow). With-oproviding full detail, digging out the numbers is slower; thus,pmemstat -omay take a few seconds to start, but, in its loop mode, refreshes are relatively fast and efficient by avoiding recomputing unchanged numbers. When in its window mode, typingotoggles low/high detail.pmemstat's grouping feature rolls up the resources of multiple processes of a feature (e.g., a browser) to make the total memory/cpu impact much more apparent.Its looping features allow monitoring for changes in memory growth which may be "leaks".  Segregating memory by types can make identifying leaks faster and more certain.In version 2.0,pmemstathas many new features including:In itswindow mode,pmemstatupdates the terminal in place (using "curses") rather than scrolling.ShowingCPU use, too, which makespmemstata viable alternative totopfor regular use (although more specialized and still focused on accurate memory representation).Supportskilling processesthat are selected visually with confirmation they are really gone (or not).And severalnew optionsthat can becontrolled dynamicallyif in window mode.Installation OptionsNote that:pmemstatneeds to run as root to read the memory statistics for all processes (which is normally desired).By default,pmemstatreruns itself as withsudo(thus you needsudoprivileges).To defeat re-running withsudo, use the--run-as-useror-Uoption.See the Quick Start at the top for preferred install instructions usingpipx. If not acceptable, see the "Alternative Installation Options" section below.Usageusage: pmemstat [-h] [-D] [-C] [-g {exe,cmd,pid}] [-f] [-k MIN_DELTA_KB]
        [-l LOOP_SECS] [-L CMDLEN] [-t TOP_PCT] [-n] [-U] [-o]
        [-u {MB,mB,KB,human}] [-R] [-s {mem,cpu,name}] [-/ SEARCH] [-W] [pids ...]

positional arguments:
  pids                  list of pids/groups (none means every accessible pid)

options:
  -h, --help            show this help message and exit
  -D, --debug           debug mode (the more Ds, the higher the debug level)
  -C, --no-cpu          do NOT report percent CPU (only in window mode)
  -g {exe,cmd,pid}, --groupby {exe,cmd,pid}
                        grouping method for presenting rows
  -f, --fit-to-window   do not overflow window [if -w]
  -k MIN_DELTA_KB, --min-delta-kb MIN_DELTA_KB
                        minimum delta KB to show again [dflt=100 if DB else 1000
  -l LOOP_SECS, --loop LOOP_SECS
                        loop interval in secs [dflt=5 if -w else 0]
  -L CMDLEN, --cmdlen CMDLEN
                        max shown command length [dflt=36 if not -w]
  -t TOP_PCT, --top-pct TOP_PCT
                        report group contributing to top pct of ptotal [dflt=100]
  -n, --numbers         show line numbers in report
  -U, --run-as-user     run as user (NOT as root)
  -o, --others          collapse shSYSV, shOth, stack, text into "other"
  -u {MB,mB,KB,human}, --units {MB,mB,KB,human}
                        units of memory [dflt=MB]
  -R, --no-rise         do NOT raise change/adds to top (only in window mode)
  -s {mem,cpu,name}, --sortby {mem,cpu,name}
                        grouping method for presenting rows
  -/ SEARCH, --search SEARCH
                        show items with search string in name
  -W, --no-window       show in "curses" window [disables: -D,-t,-L]Explanation of some options and arguments:-g {exe,cmd,pid}, --groupby {exe,cmd,pid}-  select the grouping of memory stats for reporting.exe- group by basename of the executable (the default)cmd- group by the truncated command line (use-L CMDLENto choose length)pid- group by one process-k MIN_DELTA_KB, --min-delta-kb MIN_DELTA_KB- when looping, how much change in memory use is required to show the grouping in subsequent loops; note:a positiveMIN_DELTA_KBmeans the total memory of the groupin mustgrowby that amount (in KB)a non-positiveMIN_DELTA_KBmeans the total memory of the grouping mustchangeby that amount (in KB)pids- the positional arguments may be pids (i.e., numbers) or the names of executables (as shown by-gexe)Example Usage with Explanation of OutputIn the default refreshed window loop, we seealeader linewith:the current timefrom/proc/meminfoin MemTotal (Tot), MemAvailable (Avail), Tmp (Shmem+TmpFS), and Dirty.'Oth' is the unaccounted for memory belonging to the kernel, reserve,
drivers, imprecision, etc.;Oth = Tot - Avail - Tmp - ptotal.Features such as BTRFS, ZFS, zRAM, unattached SysV Shared Memory, etc., can cause 'Oth' to be significant.Determining the contributors can be difficult, but start withsudo slabtop -scand feature specific tools (e.g.,zpool list).how many PIDs are contributing to the report vs the total number of PIDs excluding kernel threadsasecond leader line for zRAMonly if zRAM is active with:percent cpu consumed by kernel processes (not normalized ... if there 4 CPUs, then the total CPU can 400%). This is important because, with zRAM, this often is mostly the swap process.zRAM- the amount of "Used" RAM that is storing zRAM dataeTot- the effective Total RAM and the percent of "Tot" RAM (which would typically be above 100%). The biggerzRAM, the more accurate this number.eUsed- the effective Used RAM and percent of "Tot" RAM.eAvail- the effective Available RAM and percent of "Tot" RAM.aheader line with the reported fieldsincluding:cpu- percent CPU (again not normalized to 100%)pswap- proportional use of swap (per smaps_rollup)other- partly sums these categories (shown by the key,o):shSYSV- proportional use of System V shared memory (per smaps)shOth- proportional use of other shared memory (per smaps)stack- exclusive use of stack memory per smapstext- proportional use of memory for text (i.e., the read-only binary code, per smaps)data- exclusive use of memory for data (i.e., exclusively used of "heap" memory, per smaps)ptotal- proportional use of memory of all categories (i.e, sum of columns to the left);note, if zRAM, this includespswap, else not.empty- type of the entry which may be:T- the grand totalA- a newly added groupingO- combined overflow groupings below the--top-pctthreshold (only on first loop)+-{number}K- number of KB of change inptotal(only on subsequent loops)key/infowhich is a quantifier plus the grouping key. The quantifier may be:{PID}- when the grouping line represents one process (for option-gexe).{num}x- where {num} is the number of processes in the grouping.Help Screen (in Window Mode, Press '?')In window mode, press '?' to enter the help screen which looks like:Notes:There are a number of navigation keys (mostly following vim conventions); in the help screen, they apply to help screen; otherwise, they apply to main screen.Below the line, there are a number of keys/options; when you type an option key (e.g, "c"), it will highlight the next option value (e.g., "off"); when you change options, they will be applied to the next loop of the main menu.These option keys can be used in the main menu (e.g., pressing "c" will change hide or reveal the CPU column w/o entering the help screen).Kill Mode (Window Mode)Pressing "K" enter "Kill Mode" where you use the navigation keys to highlight a row, and then press ENTER to kill the process(es) represented by that row.Scroll Position (Window Mode)Sometimes, the horizontal line between the header and scrollable region has a reverse video block (under the "351" in this case):Notes:When there is no block, the scrolled document does not overflow the scrollable region.When there is a block, its position indicates:Leftmost- At the top of the document.Rightmost- At the bottom of the document.Between Leftmost and Rightmost- At percentage of the document approximated by its position from Left (0%) to Right (100%).Its length indicates, roughly, how much of the entire document you can see.Again, you can press 'f' to fit the document to the screen with a "rollup" line summarizing the lines that would not fit.Quirks and Detailspswapseems to be only provided by thesmaps_rollupsfile, and thus it may be slightly out of sync with the data gathered bysmaps.theptotal(from 'smaps') andpss(fromsmaps_rollupsand usually hidden) seem differ more than expected but still close.after the first loop,pssis read and only groups with sufficient aggregate change are probed for the details.  Thus, subsequent loops are more efficient by avoiding the reading ofsmapsin many cases (with some loss of accuracy).the "exe" value comes from the command line (based/proc/{PID}/cmdlinewhich is a bit funky). Firstly, the leading path is stripped; secondly, if the resulting executable is a script interpreter (e.g., python, perl, bash, ...) AND the first argument seems to be a full path (i.e., starts with "/"), then the "exe" will be represented as "{interpreter}->{basename(script)}".  For example, "python3->pmemstat.py" in the example above.Test Program and Test SuggestionsThe C program,memtest.cis included and can be compiled by runningcc memtest.c -o memtest.  This program:regularly allocates more memory of all typescan be run several times simultaneously,will share SysV shared memory and memory mapped files,When runningpmemstatto monitor its memory use and changes, you should use-uKBand-k{small-number}so that you can "see" the very modest memory use of the test program and its changes.Running a number of sleeps of various durations in the background in a loop, plus one foreground sleep, can make for a robustness tests with lots of processes coming and going.  There are many "races" (i.e., a process may appear in/proc, but itssmapsis gone), and this test helps ensure the races are handled properly.Alternative Installation OptionsIf thepipxinstall is not acceptable, choose the best way to install:From PyPi as non-root. You need~/.local/binon your$PATH.python -m pip install --user pmemstat
        # to uninstall: python -m pip uninstall pmemstatOrfrom PyPi as root. This makespememstatavailable to all users
with/usr/local/binon$PATH. Note:PIP_BREAK_SYSTEM_PACKAGES=1may be required on some distros.PIP_BREAK_SYSTEM_PACKAGES=1 sudo python -m pip install pmemstat
        # to uninstall: sudo python -m pip uninstall pmemstatOrfrom GitHub, scripted install. The includeddeployscript installs
a single-filepmemstatto/usr/bin/pmemstat;deployinstalls/reinstallsstickytape, too.
These commands install/updatepmemstatw/o leftovers (exceptstickytape):# NOTE: requires "git" to be installed beforehand
        cd /tmp; rm -rf pmemstat;
        git clone https://github.com/joedefen/pmemstat.git;
        ./pmemstat/deploy; rm -rf pmemstat
        # to uninstall run: sudo rm /usr/bin/pmemstatOrfrom GitHub, manual install: If you clonepmemstatfrom GitHub, then you may run itsdeployscript OR run "pip install ." as root or not OR just directly runpmemstat/main.py.