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
| Process | Started by | Holds |
|---|---|---|
| web parent | python -m librephotos.web_server | Django, every app, the URLconf; forks the web workers |
| web workers | forked from the web parent | the pages they changed since the fork, the request heap |
| task supervisor | manage.py run_workers | Django and the queue's own modules |
| fork server | the supervisor (multiprocessing forkserver) | Django and the scan pipeline (api.jobs.worker_preload) |
| task workers | forked from the fork server, on demand | the 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()importslibrephotos.asgi, thenload_urlconf()importslibrephotos.urls(every view and serializer) and builds the resolver's reverse index beforegc.freeze(). Up to 1.7.0 previews each forked worker did this on its first request. run_workersskips 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_APPShasSimpleAdminConfig, which does not import every app'sadmin.py, andlibrephotos/urls.pycallsadmin.autodiscover(). The admin's tables and models stay installed everywhere (the cascade of a deleted user reachesdjango_admin_log), and the admin stays, because the SSO settings link to its Social applications page.api/tests/infra/test_setup_footprint.pyfails whendjango.setup()loads a web-only module. - The SSO adapter is installed by the module that serves SSO.
api/views/sso.pysetsOpenIDConnectProvider.oauth2_adapter_classwhen it is imported, which the URLconf does. In 1.7.0 previewsApiConfig.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 inSIGNAL_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.decodingis inTASK_MODULESofapi/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.pyfails whendjango.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.