AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Next Dev Overrides Container-Aware V8 Heap Limit Using Host Memory

Next.js dev server calculates the V8 max-old-space-size based on host total memory (os.totalmem()), ignoring container memory constraints. In a Docker container with a memory limit, this sets an excessively high heap limit, potentially leading to out-of-memory (OOM) kills or performance degradation. Node.js already provides process.constrainedMemory() which respects cgroup limits; Next.js should use it when available.

highConfidence 95%Next.jsAffected V14.0.0Affected V15.0.0Affected V16.0.0Affected V16.3.4Affected V16.4.0-Canary.21

Origin Analysis

The heuristic in next-dev.ts (introduced in PR #57163) uses os.totalmem() * 0.5 to set --max-old-space-size, overriding Node's runtime-aware default. In containerized environments, os.totalmem() returns the host's total memory, not the container's cgroup limit, causing an inflated heap size.
1. Run a Docker container with a memory limit, e.g., docker run --rm -m 2g node:22. 2. Inside the container, create a Next.js app using create-next-app. 3. Start next dev and inspect the NODE_OPTIONS environment variable set by Next.js. 4. Observe that --max-old-space-size is set to approximately half of host memory (e.g., 15890 MB on a 32GB host) instead of a container-appropriate value (around 1048 MB).

Fixing Code Block

Edge Case Audit

process.constrainedMemory() may not be available on all Node versions or platforms; the fallback handles that. In some environments (e.g., cgroup v1 with certain configurations), constrainedMemory might return 0 or an inaccurate value, leading to the old behavior. Additionally, users who previously relied on NEXT_DISABLE_MEM_OVERRIDE or manual NODE_OPTIONS should still see those respected, as the fix only changes the default calculation. Rolling back this change would revert to the host-memory heuristic, so no special rollback steps are needed beyond reverting the code.

Ecosystem Topology