You will build a Python CLI that profiles process memory with psutil and ranks in-process allocations with tracemalloc. By the end you will produce a report that pinpoints the top memory consumers.

Learning Objectives

  • Read process RSS and system memory with psutil.
  • Rank allocation sites with tracemalloc.
  • Emit a structured report and test it.
  • Time: ~8 hours · Difficulty: Intermediate · Prereqs: Python fundamentals.

Architecture Overview

Environment Setup

  • Python 3.11 venv with psutil and pytest.
  • A sample workload to profile.

Step-by-Step Execution

01
Capture process and system memory
import psutil, os
rss = psutil.Process(os.getpid()).memory_info().rss
sysmem = psutil.virtual_memory().percent
02
Rank allocators with tracemalloc
python -m memanalyzer profile --top 5 ./workload.py
03
Run the tests
$ pytest -q
..... 5 passed in 0.07s

Progress So Far

Testing & Validation

python -m memanalyzer profile --top 5 ./workload.py && pytest -q

The report should list the top 5 allocation sites and tests should pass. If so, the analyzer correctly identifies memory hotspots.

Troubleshooting
  • tracemalloc empty: start it before the workload runs.
  • RSS looks high after frees: the allocator may retain memory; trust tracemalloc deltas.
  • Import errors: run with python -m for package resolution.

Extension Ideas

Key Results

  • Profiled process and system memory accurately.
  • Ranked the top 5 allocation sites with tracemalloc.
  • Produced a structured, testable report.
  • Backed the logic with a passing pytest suite.