Repository navigation
Conversation
SteffenDE
force-pushed
the
sd-parse-heap-size
branch
from
November 26, 2026 10:37
52dbb7a to
95a3ff2
Compare
SteffenDE
marked this pull request as ready for review
November 26, 2026 10:37
josevalim
requested changes
Nov 27, 2026
| %{conn | params: params, body_params: body_params} | ||
| end | ||
|
|
||
| defp with_max_heap_size(limit, fun) when limit in [nil, :infinity], do: fun.() |
Member
There was a problem hiding this comment.
We don't seem to document nil is supported anywhere, so I'd pattern match on infinity directly.
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. |
Member
There was a problem hiding this comment.
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.