{"id":6041,"library":"ps-mem","title":"ps-mem: Per-Program Memory Usage Reporter","description":"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.","status":"active","version":"3.14","language":"python","source_language":"en","source_url":"https://github.com/pixelb/ps_mem","tags":["system-monitoring","memory-usage","linux","cli-tool"],"install":[{"cmd":"pip install ps-mem","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"note":"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.","symbol":"main","correct":"import ps_mem\n# Then call ps_mem.main() after preparing sys.argv and redirecting stdout"}],"quickstart":{"code":"import subprocess\nimport os\n\n# Example 1: Run ps_mem as a command to get total memory usage\n# Note: ps_mem often requires root privileges for full accuracy.\n# This example tries without sudo first.\n\ntry:\n    # Using a common flag like -t for total or no flags for default output\n    result = subprocess.run(['ps_mem', '-t'], capture_output=True, text=True, check=True)\n    print(\"\\n--- ps_mem (total) output ---\")\n    print(result.stdout)\nexcept FileNotFoundError:\n    print(\"Error: 'ps_mem' command not found. Ensure it's installed and in your PATH.\")\nexcept subprocess.CalledProcessError as e:\n    print(f\"Error running ps_mem: {e}\")\n    print(f\"Stderr: {e.stderr}\")\n    print(\"Note: ps_mem often requires root privileges. Try running with sudo if you encounter permission errors.\")\n\n# Example 2: Programmatic call to ps_mem's main function (more advanced, requires mocking)\n# This is illustrative and not typically recommended for simple use cases due to sys.argv/stdout manipulation.\nimport sys\nfrom io import StringIO\nfrom unittest.mock import patch\n\n# Assuming ps_mem is installed and available in your Python environment\ntry:\n    import ps_mem\n\n    # Capture stdout and mock sys.argv\n    captured_output = StringIO()\n    with patch('sys.argv', ['ps_mem', '-S']), \\\n         patch('sys.stdout', captured_output):\n        # Call the main function with desired arguments (e.g., -S for swap info)\n        # ps_mem.main() might call sys.exit(), so wrap it if needed or catch SystemExit.\n        try:\n            ps_mem.main()\n        except SystemExit as e:\n            if e.code != 0: # 0 means successful exit\n                print(f\"ps_mem.main() exited with error code {e.code}\")\n\n    output_from_main = captured_output.getvalue()\n    print(\"\\n--- ps_mem.main() programmatic output ---\")\n    print(output_from_main)\n\nexcept ImportError:\n    print(\"Error: 'ps_mem' module not found. Install with 'pip install ps-mem'.\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred during programmatic call: {e}\")","lang":"python","description":"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`."},"warnings":[{"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', ...])`.","message":"ps-mem typically requires root privileges (e.g., running with `sudo`) to accurately access `/proc/$pid/smaps` and other system files for comprehensive memory reporting.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Only use ps-mem on Linux systems. Consider cross-platform alternatives like `psutil` if OS compatibility is required.","message":"ps-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.","severity":"gotcha","affected_versions":"All versions"},{"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`.","message":"The 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`.","severity":"gotcha","affected_versions":"All versions"},{"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`.","message":"The 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*).","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"search_vec":"'2':86 '2022':66 '3':80 '3.14':62 'accur':19 'across':49 'also':83 'belong':52 'calcul':39 'cli':95 'cli-tool':94 'command':73 'command-lin':72 'core':22 'current':58 'design':17 'distinguish':29 'given':55 'line':74 'linux':28,93 'may':65 'mem':3,12 'memori':7,23,48,91 'memory-usag':90 'monitor':89 'oper':69 'per':5,25,36 'per-process':35 'per-program':4 'primarili':76 'privat':45 'process':37,51 'program':6,26,56 'ps':2,11 'ps-mem':1,10 'python':15,79,85 'ram':40 'releas':63 'report':9,20,34 'share':47 'stabl':59 'sum':43 'support':84 'system':88 'system-monitor':87 'though':81 'tool':32,75,96 'usag':8,24,41,92 'util':16 'version':60 'written':77","created_at":"2026-04-14T18:39:23.456044+00:00","updated_at":"2026-04-16T18:13:28.144112+00:00","problems":[{"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`.","cause":"The `ps_mem` executable is not in your system's PATH, or the installation was not completed correctly.","error":"ps_mem: command not found"},{"fix":"Run the `ps_mem` command with `sudo`: `sudo ps_mem`.","cause":"`ps-mem` requires root privileges to access `/proc/$pid/smaps` and other system files for accurate memory reporting.","error":"Permission denied"},{"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)`.","cause":"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`.","error":"ImportError: No module named 'ps_mem'"},{"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`.","cause":"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`.","error":"/usr/bin/env: 'python': No such file or directory"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"3.14","cli_name":"ps-mem","cli_version":"sh: 1: ps-mem: not found","type":"library","homepage":null,"github":"http://github.com/pixelb/ps_mem","docs":null,"changelog":null,"pypi":"https://pypi.org/project/ps-mem/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["devops","observability"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-06-28","next_check":"2026-07-28","install_tag":null}}