Registry / devops / ps-mem

ps-mem

JSON →
library3.14pypypi✓ verified 89d ago

ps-mem is a Python utility designed to accurately report the core memory usage per program on Linux, distinguishing itself from tools that report per-process. It calculates RAM usage by summing both private and shared memory across all processes belonging to a given program. The current stable version is 3.14, released in May 2022, and it operates as a command-line tool, primarily written in Python 3, though it also supported Python 2.

pip install ps-mem
INSTALL
IMPORT
SIG · PS-MEM
P
ps-mem
devopspythonv3.14
Install
1.5s avg
Import
8ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v3.14 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.007s · 17.8MB
glibc
py 3.10–3.920 runs
installs and imports cleanly · install 1.5s · import 0.006s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

main
✓ import ps_mem # Then call ps_mem.main() after preparing sys.argv and redirecting stdout
ps-mem is primarily a command-line tool. While you can import and call its 'main' function, it is designed to parse command-line arguments (sys.argv) and print directly to sys.stdout. Programmatic interaction often involves running it as a subprocess to capture output or carefully mocking sys.argv and stdout for direct calls.

The primary way to use ps-mem programmatically is to execute it as an external command using `subprocess`, mimicking its typical command-line usage. A more advanced method involves directly calling `ps_mem.main()`, but this requires careful handling of `sys.argv` and `sys.stdout`.

import subprocess import os # Example 1: Run ps_mem as a command to get total memory usage # Note: ps_mem often requires root privileges for full accuracy. # This example tries without sudo first. try: # Using a common flag like -t for total or no flags for default output result = subprocess.run(['ps_mem', '-t'], capture_output=True, text=True, check=True) print("\n--- ps_mem (total) output ---") print(result.stdout) except FileNotFoundError: print("Error: 'ps_mem' command not found. Ensure it's installed and in your PATH.") except subprocess.CalledProcessError as e: print(f"Error running ps_mem: {e}") print(f"Stderr: {e.stderr}") print("Note: ps_mem often requires root privileges. Try running with sudo if you encounter permission errors.") # Example 2: Programmatic call to ps_mem's main function (more advanced, requires mocking) # This is illustrative and not typically recommended for simple use cases due to sys.argv/stdout manipulation. import sys from io import StringIO from unittest.mock import patch # Assuming ps_mem is installed and available in your Python environment try: import ps_mem # Capture stdout and mock sys.argv captured_output = StringIO() with patch('sys.argv', ['ps_mem', '-S']), \ patch('sys.stdout', captured_output): # Call the main function with desired arguments (e.g., -S for swap info) # ps_mem.main() might call sys.exit(), so wrap it if needed or catch SystemExit. try: ps_mem.main() except SystemExit as e: if e.code != 0: # 0 means successful exit print(f"ps_mem.main() exited with error code {e.code}") output_from_main = captured_output.getvalue() print("\n--- ps_mem.main() programmatic output ---") print(output_from_main) except ImportError: print("Error: 'ps_mem' module not found. Install with 'pip install ps-mem'.") except Exception as e: print(f"An unexpected error occurred during programmatic call: {e}")
ps-mem --version
Debug
Known issues
gotchaps-mem typically requires root privileges (e.g., running with `sudo`) to accurately access `/proc/$pid/smaps` and other system files for comprehensive memory reporting.
fix
Execute the `ps_mem` command with `sudo` or ensure the script is run with appropriate permissions. For programmatic use via `subprocess`, ensure the command includes `sudo` if necessary, e.g., `subprocess.run(['sudo', 'ps_mem', ...])`.
affects: All versions
gotchaps-mem is a Linux-specific utility due to its reliance on the `/proc` filesystem (e.g., `/proc/$pid/smaps`) for memory information. It will not work on other operating systems like macOS or Windows.
fix
Only use ps-mem on Linux systems. Consider cross-platform alternatives like `psutil` if OS compatibility is required.
affects: All versions
gotchaThe library is primarily designed as a standalone command-line script, not a Python library with a traditional programmatic API of classes and functions. Directly importing `ps_mem` and calling internal functions like `ps_mem.main()` requires careful manipulation of `sys.argv` and `sys.stdout`.
fix
For most programmatic uses, consider running `ps_mem` as a subprocess and parsing its standard output. If direct import is necessary, be prepared to mock `sys.argv` and redirect `sys.stdout`.
affects: All versions
gotchaThe developer notes that the name 'ps_mem' is for backwards compatibility, and 'coremem' would be a more accurate description of what the tool does (reports core memory usage per *program*, not per *process*).
fix
Understand that the tool aggregates memory usage by program name, not by individual process IDs, which is a key distinction from other memory reporting utilities like `ps`.
affects: All versions
Errors
Common errors & fixes
ps_mem: command not found
The `ps_mem` executable is not in your system's PATH, or the installation was not completed correctly.
fix
Ensure `ps-mem` is installed via `pip install ps-mem` or by downloading the script and placing it in a directory included in your PATH, such as `/usr/local/bin`, then making it executable: `sudo chmod +x /usr/local/bin/ps_mem`.
Permission denied
`ps-mem` requires root privileges to access `/proc/$pid/smaps` and other system files for accurate memory reporting.
fix
Run the `ps_mem` command with `sudo`: `sudo ps_mem`.
ImportError: No module named 'ps_mem'
The `ps-mem` Python package is either not installed in the current Python environment, or you are attempting to import it as a standard library when it's primarily designed as a command-line utility. The package name for `pip` is `ps-mem`, but the module is `ps_mem`.
fix
Install the package using pip: `pip install ps-mem`. If you intend programmatic use, remember it's primarily a command-line tool, and direct module import might require careful handling; running it as a subprocess is often recommended: `import subprocess; result = subprocess.run(['sudo', 'ps_mem'], capture_output=True, text=True, check=True); print(result.stdout)`.
/usr/bin/env: 'python': No such file or directory
This error typically occurs on Linux systems where the default `python` command points to `python2` or is missing, but `ps-mem` (a Python 3 script) expects `python3`.
fix
Create a symbolic link for `python` to point to `python3`: `sudo ln -s /usr/bin/python3 /usr/bin/python`. Alternatively, if running the script directly, explicitly use `python3 ps_mem.py`.
Upgrade
Version history
3.14latest on PyPI · released May 28, 2022
Audit
Dependencies

No dependency data recorded yet.

Agent activity
33 hits · last 30 days
node
30
OpenAI (training)
2
Anthropic
1
Resources
ps-mem — pip install ps-mem · libregistry