About Profiling Code Memory Usage

You can profile your code’s memory usage with memray (install it from PyPI). When you run a profiling session, it generates a *.bin file:

(venv)$ memray run manage.py my_management_command

After that, create an HTML report like this:

(venv)$ memray flamegraph *.bin

Open the generated file (something like memray-flamegraph-manage.py.*.html) in your browser. Use the search bar to look for handle or any other function or method you want to inspect.

One surprising detail: your own script might use less than 0.05% of the total memory, while most of it comes from imported modules and Django’s initialization process.

Tips and Tricks Programming Development Optimization Django 6.x Django 5.2 Django 4.2 Python 3 memray