Memray is a memory profiler for Python that helps track memory allocation and deallocation through flame graphs. To profile a Django project, start by running `memray run manage.py check` to measure startup memory usage, then generate a flame graph with `memray flamegraph`. The visualization shows memory allocations over time, with frame width proportional to memory used. Common optimizations include deferring expensive imports like numpy until needed, using lazy imports, or replacing heavy dependencies with lighter alternatives. A practical example demonstrates reducing startup memory from 25.2 MB to 19.4 MB by replacing numpy's shuffle with Python's built-in random.shuffle().

6m read timeFrom adamj.eu
Post cover image
Table of contents
Profile a Django projectMake an improvementA Zsh one-liner to speed up checking resultsFin

Sort: