n8n JavaScript Heap Out of Memory Docker: Diagnose & Fix
Diagnose n8n JavaScript heap out of memory in Docker, set NODE_OPTIONS safely, verify the limit, and separate heap errors clearly from Docker OOMKilled.
In an n8n Docker container, “JavaScript heap out of memory” means Node.js hit its V8 heap limit while running a workflow. Confirm that first. Then set NODE_OPTIONS=--max-old-space-size=... in the Docker deployment configuration, recreate the container, and retest while watching container and host memory. If the workflow or its concurrency is too large, reduce that load instead of treating a larger heap as the complete fix.
Identify which memory limit failed
First, identify whether Node.js, Docker, or the host ran out of memory. A fatal JavaScript heap message suggests V8 heap exhaustion. OOMKilled suggests Docker or the kernel killed the container. A lost connection or HTTP 503 alone does not identify the cause.
Save the logs and execution details before changing settings. Check recent container output and Docker’s recorded state.
- Read the recent n8n logs.
docker logs --tail 100 n8nA message containing JavaScript heap out of memory or Reached heap limit supports a V8 heap diagnosis. The exact wording can vary by Node.js version and failure path.
- Check whether Docker recorded an out-of-memory kill.
docker inspect n8n --format='{{.State.OOMKilled}} {{.State.ExitCode}}'true for OOMKilled means Docker reports that the container was killed because of an out-of-memory condition. An exit code alone does not prove the cause.
- Watch memory while reproducing the problem.
docker stats n8nA climb toward the container limit supports a container-budget or workload-capacity problem. A fatal V8 message while Docker still shows room can mean Node.js hit its heap limit first. Both conditions can occur in one deployment.
- Check the host as well. Look for low available RAM, swapping, and kernel OOM messages. The host can be under pressure even when n8n is below its container limit. If the host begins swapping heavily, unrelated services are killed, or the container repeatedly restarts, stop the workload. Save the evidence before changing more settings.
| Signal | More likely explanation | Next check |
|---|---|---|
| Fatal JavaScript heap message | Node.js V8 heap limit | Verify NODE_OPTIONS and inspect the workflow peak |
| OOMKilled is true | Container or host memory kill | Check the Docker limit, docker stats, and host pressure |
| Memory reaches the container limit | Container budget is too small for the peak | Reduce workload or concurrency before raising limits |
| Host swaps or other services fail | Insufficient host RAM or competing services | Stop the test and review the total host budget |
Before changing the heap setting
Use the deployment configuration that creates the n8n container. Changing an environment variable in your current shell or in a running process does not change an existing container. First, check the current setting, memory limit, and rollback path.
- Inspect the environment received by the existing container.
docker inspect n8n --format='{{range .Config.Env}}{{println .}}{{end}}' | grep '^NODE_OPTIONS='If nothing prints, the running container did not receive NODE_OPTIONS. If an older value prints, the deployment file has not been applied.
- Record the current container statistics and configured memory limit.
docker inspect n8n --format='{{.HostConfig.Memory}}'
docker stats --no-stream n8nA zero value for the configured memory field can mean Docker has no explicit container memory limit. It does not mean the host has unlimited usable RAM.
- Preserve a rollback path. Keep the previous Compose file or deployment definition. Do not remove containers or volumes as a first response. They may contain configuration or execution data. Adjust execution-data retention only with reference to your recovery requirements and the current n8n documentation. Do not delete execution data blindly.
Raise Node.js heap in Docker
Raise the heap only when the checks point to V8 exhaustion and the container and host have headroom. --max-old-space-size uses megabytes. A value of 2048 asks Node.js to allow about 2 GB for its V8 old-generation heap. It does not reserve that amount. It also excludes memory used by n8n, Node.js, libraries, buffers, the operating system, and other processes.
Do not copy 2048 blindly. The heap value must fit within the container’s available memory. Leave room for non-heap memory and services such as PostgreSQL, Redis, workers, and the reverse proxy. If the host is already short of RAM, a larger heap can cause an OOM kill, heavy swapping, or a wider outage.
- For Docker Compose, place the setting under the n8n service’s environment section.
services:
n8n:
image: n8nio/n8n:latest
environment:
- NODE_OPTIONS=--max-old-space-size=2048The image tag and Compose resource-limit syntax must match your deployment. Check the current Docker Compose and n8n documentation if another configuration layer supplies environment variables or memory limits.
- For a standalone container, pass the same variable when creating the container.
docker run --name n8n -e NODE_OPTIONS=--max-old-space-size=2048 n8nio/n8n:latest- Recreate the container after changing the environment configuration.
docker compose up -d --force-recreate n8nUse the project directory and service name from your deployment. Recreating reliably applies changed container environment settings. Changing the variable only in your shell does not alter an existing container. Do not remove volumes during this step.
- Verify the setting in the running container.
docker exec n8n printenv NODE_OPTIONS
docker exec n8n node -p "process.execArgv"
docker exec n8n node -e "console.log(process.env.NODE_OPTIONS); console.log(process.memoryUsage())"The first command should print the configured value. process.execArgv can omit options supplied through NODE_OPTIONS in some Node.js versions. The environment value is therefore the clearest check that the container received the setting. The memory command shows current process information, not the workload peak.
- If the expected value is missing, do not treat the change as applied. Inspect the rendered Compose configuration or platform deployment settings. Correct the source configuration, recreate the container again, and repeat the checks.
Reduce workflow memory before raising the limit again
A larger heap does not make a large in-memory workload cheap. Review the failed execution and find the node where item counts, JSON size, or binary content first expands.
Common memory-heavy patterns include:
- Fetching a whole dataset instead of using pagination, filters, or a smaller response.
- A Code node copying items, repeatedly joining arrays, or building a second large array while the first remains live.
- Keeping every result in memory instead of processing smaller batches and releasing references.
- Converting files to base64 or carrying binary data through nodes that do not need it.
- Retaining large inputs and outputs in execution history longer than operational needs require.
- Starting another scheduled execution before the previous one finishes.
Reduce the peak before increasing the limit again. Paginate upstream calls, filter fields, split batches, avoid unnecessary Code-node copies, and stream or externalize large files where the integration supports it. Processing one customer, file, or page at a time is generally easier to budget than one large in-memory collection.
Lower concurrency while testing. One execution may fit, yet several overlapping executions may fail. If the same workflow shape repeatedly creates the spike, profile or rewrite the Code node, reduce the payload, or redesign the workflow. Do not use a larger heap only to delay the failure.
Account for queue mode, workers, task runners, and concurrency
Queue mode does not remove memory costs. It changes where some work runs. The main n8n process, each worker, task runners, Redis, PostgreSQL, and Docker overhead all need memory. More workers or higher worker concurrency can multiply simultaneous workflow peaks.
Task runners can also change where Code-node work occurs. Check the current n8n documentation for your n8n version and deployment mode before assuming memory belongs only to the main container.
Build the budget around intended peak use, not idle usage. Include the main process, every worker, task-runner processes, queue and database services, other containers, and the operating system. If the container or host becomes unstable when workers or concurrency increase, lower those settings first. Add host memory only after measuring the total deployment requirement and confirming that the workload itself is not unnecessarily large.
Validate the fix safely
Validate the change as a controlled test. Use a representative path and payload shape, but reduce item count or concurrency first if the test could affect production data. Keep the old deployment definition so you can revert.
- Run the smallest representative workload. Use the node path that triggered the error and an approximate payload shape. Avoid a destructive production run unless you have a recovery path.
- Follow the n8n logs during the run.
docker logs -f n8nA useful result is a completed execution without a new fatal heap message or repeated restart. A clean log does not prove that the host has enough capacity.
- Watch the container during the peak.
docker stats n8nRecord the memory trend instead of relying on an idle snapshot. Check host RAM and swap, plus PostgreSQL, Redis, workers, and other containers. Stop the test if the container reaches its limit, the host starts swapping heavily, or unrelated services become unstable.
- Inspect execution data. Identify where item counts, JSON size, or binary content expands. Confirm that execution-history retention still matches operational and recovery needs. A successful run is not a complete fix if it leaves an unsafe amount of data behind.
- Test the intended overlap. Run the workflow at its real schedule and concurrency. Include queue mode, worker count, task-runner settings, and per-worker concurrency. These settings belong in the same memory calculation.
- Verify the deployment configuration after a normal redeploy. Confirm that NODE_OPTIONS, concurrency settings, worker settings, and workflow changes still exist after the deployment process creates the container again.
If the heap message disappears but Docker reports OOMKilled, the host swaps, or unrelated services become unstable, the heap change did not solve the problem. Revert it, save the logs and statistics, and address the workflow peak, container budget, or host capacity instead.
Conclusion
Start with the evidence. Distinguish a V8 heap failure from a container or host memory kill. Then apply NODE_OPTIONS through the Docker deployment configuration, recreate and verify the container, and retest the real workload. If the workload remains too large or too concurrent, reduce it before raising the limit again.
Frequently Asked Questions
Do I need to recreate the n8n container after changing NODE_OPTIONS?
Normally, yes. Update the deployment configuration, recreate the container, and verify the result with docker exec n8n printenv NODE_OPTIONS. Changing a variable in your current shell does not modify an existing container.
What is the difference between JavaScript heap out of memory and OOMKilled?
A JavaScript heap error means Node.js could not allocate more managed V8 heap space. OOMKilled means Docker reports that the container was killed because of an out-of-memory condition. The first often leaves a fatal JavaScript message. The second can appear as an abrupt stop, so check logs, Docker state, container statistics, and host memory together.
Should I raise the heap or lower n8n concurrency?
Lower concurrency when executions overlap or the container and host are near their limits. Raise the heap only when the evidence points to V8 reaching its limit and safe headroom exists. Queue mode, workers, task runners, and concurrency multiply simultaneous memory use, so a larger heap can worsen an unstable deployment.
Sources
- n8n Workflow Running Out of Memory? Find and Fix the Cause (accuweb.cloud)