Skip to content

Allow setting a maximum heap size during request parsing - #1342

Open
SteffenDE wants to merge 2 commits into
elixir-plug:mainfrom
SteffenDE:sd-parse-heap-size
Open

SteffenDE wants to merge 2 commits into
elixir-plug:mainfrom
SteffenDE:sd-parse-heap-size

Conversation

@SteffenDE

@SteffenDE SteffenDE commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Limits the maximum heap size during request parsing. Defaults to 32MB (on 64bit machines) which is plenty of space for regular requests. There is no meaningful overhead.

  • dense flat JSON
  ┌─────────┬─────────┬───────────┬─────────────┐
  │ fields  │  body   │ peak heap │ fits 32 MB? │
  ├─────────┼─────────┼───────────┼─────────────┤
  │ 10,000  │ 0.13 MB │ 2.5 MB    │ yes         │
  ├─────────┼─────────┼───────────┼─────────────┤
  │ 50,000  │ 0.73 MB │ 17.4 MB   │ yes         │
  ├─────────┼─────────┼───────────┼─────────────┤
  │ 100,000 │ 1.48 MB │ 22.7 MB   │ yes         │
  ├─────────┼─────────┼───────────┼─────────────┤
  │ 200,000 │ 3.18 MB │ 51.3 MB   │ NO (killed) │
  └─────────┴─────────┴───────────┴─────────────┘
  • Big off-heap value (e.g. a large base64 field) + small envelope
  ┌────────────────────────────────────┬────────┬───────────┬─────────────┐
  │              Payload               │  body  │ peak heap │ fits 32 MB? │
  ├────────────────────────────────────┼────────┼───────────┼─────────────┤
  │ value ~1.5 MB base64 + 500 fields  │ 1.5 MB │ 0.1 MB    │ yes         │
  ├────────────────────────────────────┼────────┼───────────┼─────────────┤
  │ value ~7.5 MB base64 + 500 fields  │ 7.5 MB │ 0.1 MB    │ yes         │
  ├────────────────────────────────────┼────────┼───────────┼─────────────┤
  │ value ~7.0 MB base64 + 2000 fields │ 7.0 MB │ 0.6 MB    │ yes         │
  └────────────────────────────────────┴────────┴───────────┴─────────────┘
  • Dense, structured JSON with no large off-heap binaries
  ┌──────────────────────────────┬───────────┬────────┬───────────────────────┐
  │    ~7.4 MB JSON              │ peak heap │ factor │      fits 32 MB?      │
  ├──────────────────────────────┼───────────┼────────┼───────────────────────┤
  │ array of integers            │ 240 MB    │ 32×    │ no — killed           │
  ├──────────────────────────────┼───────────┼────────┼───────────────────────┤
  │ array of small objects       │ 125 MB    │ 19×    │ no — killed           │
  ├──────────────────────────────┼───────────┼────────┼───────────────────────┤
  │ array of short strings       │ 152 MB    │ 17×    │ no — killed           │
  └──────────────────────────────┴───────────┴────────┴───────────────────────┘
  • Parse times
  ┌───────────────────────────┬────────┬──────────┬──────────┬────────────────┐
  │    Legitimate request     │  size  │ default  │  +limit  │     delta      │
  ├───────────────────────────┼────────┼──────────┼──────────┼────────────────┤
  │ JSON tiny event           │ 25 B   │ 1.33 µs  │ 1.46 µs  │ +137 ns (+10%) │
  ├───────────────────────────┼────────┼──────────┼──────────┼────────────────┤
  │ JSON single object        │ 60 B   │ 1.63 µs  │ 1.70 µs  │ +68 ns         │
  ├───────────────────────────┼────────┼──────────┼──────────┼────────────────┤
  │ JSON API list, 1000 obj   │ 66 KB  │ 455 µs   │ 457 µs   │ +0.4%          │
  ├───────────────────────────┼────────┼──────────┼──────────┼────────────────┤
  │ JSON ~7.4 MB blob         │ 7.4 MB │ 7.81 ms  │ 7.92 ms  │ +1.5% (noise)  │
  ├───────────────────────────┼────────┼──────────┼──────────┼────────────────┤
  │ urlencoded small form     │ 59 B   │ 2.39 µs  │ 2.46 µs  │ +77 ns         │
  ├───────────────────────────┼────────┼──────────┼──────────┼────────────────┤
  │ urlencoded 40k-field form │ 497 KB │ 25.76 ms │ 25.95 ms │ +0.7%          │
  ├───────────────────────────┼────────┼──────────┼──────────┼────────────────┤
  │ multipart small form      │ 639 B  │ 15.58 µs │ 15.79 µs │ +211 ns        │
  └───────────────────────────┴────────┴──────────┴──────────┴────────────────┘

@SteffenDE
SteffenDE marked this pull request as ready for review November 26, 2026 10:37
Comment thread lib/plug/parsers.ex
%{conn | params: params, body_params: body_params}
end

defp with_max_heap_size(limit, fun) when limit in [nil, :infinity], do: fun.()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't seem to document nil is supported anywhere, so I'd pattern match on infinity directly.

Comment thread lib/plug/parsers.ex
Comment on lines +169 to +207
element of a JSON list of small integers, such as `1,`, takes 2 bytes in
the body and 16 bytes once decoded on 64-bit systems. A decoder may also
need several times the size of the decoded data while it runs.

`Plug.Parsers` sets a `:max_heap_size` while it parses a request, defaulting
to `4_000_000` words (roughly 32MB). You can raise it, lower it, or disable
it:

plug Plug.Parsers,
parsers: [:urlencoded, :multipart, :json],
json_decoder: Jason,
max_heap_size: 8_000_000

# or, to turn the limit off entirely
plug Plug.Parsers, parsers: [...], max_heap_size: :infinity

The size is in words (8 bytes on 64-bit systems). It is headroom on top of the
heap the process already holds when parsing starts, not an absolute ceiling:
the current heap size is read and the configured size is added to it. This way
a process that already holds memory, such as garbage left over from an earlier
request on the same keep-alive connection, is not killed while parsing a small
body.

If the limit is exceeded, the process is killed, so no error is raised and no
response is sent by `Plug.Parsers`. The web server decides how the connection
ends, and a warning is logged. Large binaries, such as the raw request body
and large string values, are stored outside of the process heap and, by
default, do not count towards the limit.

Pick the limit against the memory a single request legitimately needs to
parse, not the raw body size. A body made mostly of one large value (an
upload, a base64 field) costs almost no heap, because that value stays off the
heap. A body made of many small elements costs much more: decoding is a few
times the decoded size, and structured data expands further, so tens of
thousands of small fields or list elements are needed before the default
limit is reached. To keep a limit from killing legitimate large requests,
size it above the heap those requests need (or set `:infinity`), and pass a
map such as `%{size: 4_000_000, kill: true, error_logger: false}` if you want
to tune the kill or silence the log.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
element of a JSON list of small integers, such as `1,`, takes 2 bytes in
the body and 16 bytes once decoded on 64-bit systems. A decoder may also
need several times the size of the decoded data while it runs.
`Plug.Parsers` sets a `:max_heap_size` while it parses a request, defaulting
to `4_000_000` words (roughly 32MB). You can raise it, lower it, or disable
it:
plug Plug.Parsers,
parsers: [:urlencoded, :multipart, :json],
json_decoder: Jason,
max_heap_size: 8_000_000
# or, to turn the limit off entirely
plug Plug.Parsers, parsers: [...], max_heap_size: :infinity
The size is in words (8 bytes on 64-bit systems). It is headroom on top of the
heap the process already holds when parsing starts, not an absolute ceiling:
the current heap size is read and the configured size is added to it. This way
a process that already holds memory, such as garbage left over from an earlier
request on the same keep-alive connection, is not killed while parsing a small
body.
If the limit is exceeded, the process is killed, so no error is raised and no
response is sent by `Plug.Parsers`. The web server decides how the connection
ends, and a warning is logged. Large binaries, such as the raw request body
and large string values, are stored outside of the process heap and, by
default, do not count towards the limit.
Pick the limit against the memory a single request legitimately needs to
parse, not the raw body size. A body made mostly of one large value (an
upload, a base64 field) costs almost no heap, because that value stays off the
heap. A body made of many small elements costs much more: decoding is a few
times the decoded size, and structured data expands further, so tens of
thousands of small fields or list elements are needed before the default
limit is reached. To keep a limit from killing legitimate large requests,
size it above the heap those requests need (or set `:infinity`), and pass a
map such as `%{size: 4_000_000, kill: true, error_logger: false}` if you want
to tune the kill or silence the log.
element of a JSON list of small integers, such as `1`, takes 2 bytes in
the body and 16 bytes once decoded on 64-bit systems. A decoder also
uses additional memory while it runs.
`Plug.Parsers` sets a `:max_heap_size` while it parses a request, defaulting
to `4_000_000` words (roughly 32MB). You can raise it, lower it, or disable
it:
plug Plug.Parsers,
parsers: [:urlencoded, :multipart, :json],
json_decoder: Jason,
max_heap_size: 8_000_000
# or, to turn the limit off entirely
plug Plug.Parsers, parsers: [...], max_heap_size: :infinity
The current heap size is read and the configured size is added to it. This way
a process that already holds memory, such as existing data or garbage left from
an earlier request on the same connection, is not killed while parsing a small
body.
If the limit is exceeded, the process is killed, so no error is raised and no
response is sent by `Plug.Parsers`. The web server decides how the connection
ends, and a warning is logged. Large binaries, such as the raw request body
and large string values, are stored outside of the process heap and, by
default, do not count towards the limit.
To keep a limit from killing legitimate large requests, size it above the heap
those requests need (or set `:infinity`). Profile and benchmark if necessary.
Those limits are also enabled during tests, so representative tests will help
avoid false positives in production. You may also consider setting it to
`%{size: 4_000_000, kill: false, error_logger: true}` during an initial deployment,
to validate how frequently your production deployments will run into this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants