Memory Analyzer CLI Application in Python
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
graph LR
CLI[argparse CLI] --> Sys[psutil: RSS / system]
CLI --> Trace[tracemalloc: top allocators]
Sys --> Rep[Report]
Trace --> Rep
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.py03
Run the tests
$ pytest -q
..... 5 passed in 0.07s
Progress So Far
graph LR
A[01 Capture] -->|done| B[02 Rank]
B -->|done| C[03 Tests]
style A fill:#1a4a1a,stroke:#00ff00,color:#fff
style B fill:#1a4a1a,stroke:#00ff00,color:#fff
style C fill:#1a4a1a,stroke:#00ff00,color:#fff
Testing & Validation
python -m memanalyzer profile --top 5 ./workload.py && pytest -qThe 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 -mfor package resolution.
Extension Ideas
- Add JSON output for ingestion by dashboards.
- Compare with the lab in Memory Usage Analyzer.
- Schedule it via systemd timers.
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.