Skip to main content

Memory of the Django processes

Not released yet (comes after 1.7.0) This page describes what the Django processes of a Docker install hold in memory and the rules that keep it down. The numbers come from the 19,118-photo benchmark library (159 files, cloned 120 times) in WSL Docker with WEB_CONCURRENCY=2.

The processes​

ProcessStarted byHolds
web parentpython -m librephotos.web_serverDjango, every app, the URLconf; forks the web workers
web workersforked from the web parentthe pages they changed since the fork, the request heap
task supervisormanage.py run_workersDjango and the queue's own modules
fork serverthe supervisor (multiprocessing forkserver)Django and the scan pipeline (api.jobs.worker_preload)
task workersforked from the fork server, on demandthe pages they changed, the task heap

A page loaded before a fork is shared until a process writes to it. CPython writes to most objects it touches (reference counts, the specialised bytecode of a function it runs, a free block in a memory pool), so a web worker still copies about 70 MB of its parent's pages within its first requests. Loading a module in the parent instead of in each worker saves its size once per worker; loading it in neither saves it everywhere.

What each process loads​

  • The web parent loads the URLconf. librephotos.web_server.preload() imports librephotos.asgi, then load_urlconf() imports librephotos.urls (every view and serializer) and builds the resolver's reverse index before gc.freeze(). Up to 1.7.0 previews each forked worker did this on its first request.
  • run_workers skips Django's system checks (requires_system_checks = ()). The URL check imported the whole URLconf into the supervisor, which never serves a request. migrate, which the entrypoint runs first, still runs every check.
  • The admin is registered by the URLconf. INSTALLED_APPS has SimpleAdminConfig, which does not import every app's admin.py, and librephotos/urls.py calls admin.autodiscover(). The admin's tables and models stay installed everywhere (the cascade of a deleted user reaches django_admin_log), and the admin stays, because the SSO settings link to its Social applications page. api/tests/infra/test_setup_footprint.py fails when django.setup() loads a web-only module.
  • The SSO adapter is installed by the module that serves SSO. api/views/sso.py sets OpenIDConnectProvider.oauth2_adapter_class when it is imported, which the URLconf does. In 1.7.0 previews ApiConfig.ready() imported it, and with it allauth's views and drf-spectacular, into every process.
  • Signal modules load no scan code. django.setup() imports every module in SIGNAL_MODULES; those import the scan pipeline, numpy and pyvips only inside the receiver that needs them. The fork server imports them on purpose (api.thumbnails.decoding is in TASK_MODULES of api/jobs/worker_preload.py), so the task workers share one copy instead of each importing its own on its first photo. api/tests/infra/test_setup_footprint.py fails when django.setup() loads one of them.

Between tasks​

A task worker hands the free part of its heap back to the system after its tasks (malloc_trim, see the task queue), so the memory a scan freed is not held until the worker exits.

Measuring​

memray runs on Linux only, so run it in the backend container. Install it into a directory of its own and take it off sys.path after importing it, or its own dependencies (Jinja2, Pygments) show up in Django's imports: django.test.utils and rest_framework.compat import them when they exist.

docker exec backend pip install --target /tmp/memray memray
docker exec -it backend python
import sys
sys.path.append("/tmp/memray"); import memray; sys.path.remove("/tmp/memray")
import os; os.environ.setdefault("DJANGO_SETTINGS_MODULE", "librephotos.settings.production")
with memray.Tracker("/tmp/web.bin", trace_python_allocators=True):
import librephotos.asgi

memray.FileReader("/tmp/web.bin").get_leaked_allocation_records() lists what was still allocated when the tracker stopped, with its Python stack. Track one phase at a time: the import, then (with the import done outside the tracker) the first requests, then later requests, which shows growth. The innermost <module> frame of a stack is the module whose import made the allocation; the code objects of a module are allocated under _compile_bytecode, attributed to the module that imports it.