Memory pools and request lifecycle
Memory has a lifetime
A pool is a collection of allocations associated with an owning object. Nginx uses pools to simplify cleanup: small allocations are grouped, and larger ones may be tracked separately for release. A request can own temporary header parsing or module data; a connection can outlive that request. Configuration-cycle data and explicitly shared memory serve different lifetimes. Pool size is an allocation tuning knob, not a maximum request size. This conceptual picture uses two sequential HTTP/1.1 requests on one persistent connection.
connection lifetime: |-------------------------|request 1 pool: |-------| destroyrequest 2 pool: |-------| destroyFollow a versioned source outline
For the upstream release-1.26.3 source, src/http/ngx_http_request.c is the request-processing starting point. The names below are reading landmarks, not a stable extension ABI or exact call graph. Request creation, processing and finalization have branches for errors, asynchronous work and subrequests. Inspect the release tag matching a lab binary; distro patches may differ. Do not retain a pointer into a destroyed pool in a worker-global structure. This outline is for reading only; it is not compilable module code.
src/http/ngx_http_request.c ngx_http_create_request ngx_http_process_request ngx_http_finalize_request ngx_http_free_requestDistinguish completion from connection closure
An HTTP request may finish while its connection is eligible for reuse. An internal redirect or subrequest can make request finalization more complex than one callback returning. Cleanup should be tied to ownership rather than a guessed moment when bytes are sent. In the optional lab, two GETs reuse a connection if the server allows it; the same worker PID by itself would not prove reuse. Compare the connection identifier and request count from the response. HTTP/2 multiplexing is a separate case and this lab forces HTTP/1.1.
location = /identity { default_type text/plain; return 200 "$connection:$connection_requests\n";}Investigate memory without guessing
A growing RSS is not enough to prove a memory leak: allocator retention, long-lived connections, caches and shared pages can affect observations. Compare controlled workloads, connection counts and worker generations; do not sum RSS as though every shared page were unique. In this synthetic table a worker returns to a steady plateau rather than increasing on every completed batch. That does not prove the absence of a leak. Never tune pool sizes or force termination solely from one snapshot.
phase synthetic RSSidle 12 MiBfirst batch 18 MiBafter idle 15 MiBsecond batch 18 MiBafter idle 15 MiBPractice and recap
Sketch two requests on one persistent connection; mark when temporary request data is invalid. Explain why a single RSS reading is insufficient. Check your reasoning against the manual exercise. Next: Worker tuning, OS limits and resource capacity.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Memory pools and request lifecycle
Identify request-scoped allocations conceptually
Which object can outlive a sequential HTTP/1.1 request?
What should happen to a pointer into a destroyed request pool?
Is request pool size a request body limit?
What does a steady RSS plateau prove?
Which evidence best identifies HTTP/1.1 connection reuse in this lab?