Checking Memory Usage in a Flask Application with Scalene
1. Install and Verify Scalene
Ensure you’re using Python 3.6 or newer.
Install Scalene via pip:
pip install scaleneVerify the install:
scalene --version
2. Profile Your Flask App at Launch
If you normally start your app with python app.py, simply prepend scalene:
scalene --profile-interval=10 app.py
– --profile-interval=10 tells Scalene to dump a new profile report every ten seconds
– By default, each dump shows per-line memory use, Python vs. native time, and system time
3. Profile via Flask’s CLI
When you run your app with flask run, invoke Python under Scalene:
scalene --profile-interval=10 -- python -m flask run
– Everything the Flask CLI does (werkzeug server, your code, extensions) will get sampled
– Use --reduced-profile if you only care about total memory allocations and want a cleaner view
4. Attach to a Running Flask Process
If your app is already running (e.g., in a Docker container or systemd), find its PID:
ps aux | grep flask
Then tell Scalene to sample that process:
scalene --profile-interval=5 --pid=12345
Scalene will print a fresh profile every five seconds without restarting your server.
5. Interpreting the Output
Each report shows columns per source line:
- Time % Python: time spent in your Python code
- Time % native: time in C extensions or standard-library modules
- Memory (MB): net memory allocated (green: low, yellow: moderate, red: high)
- Sys %: time the OS spent doing other tasks
Use the coloration and rates to spot “hot” lines that steadily allocate memory.
Beyond Basic Memory Profiling
- Combine Scalene with CPU profiling flags (
--cpu-only,--native) to focus on performance bottlenecks. - Output an interactive HTML report with
--outfile=profile.htmland open it in a browser. - For long-lived services, adjust
--profile-intervalto balance detail vs. overhead. - Integrate with your CI pipeline: run short-interval Scalene profiles on PRs to catch regressions automatically.

Comments
Post a Comment