Replies: 6 comments 15 replies
|
memory leak in next.js with k8s usually comes from few sources:
// next.config.js
module.exports = {
env: {
CUSTOM_VAR: process.env.CUSTOM_VAR,
},
}
const envValue = useMemo(() => ({ ...env }), [])
export async function GET() {
return Response.json({ status: "ok" })
}
// next.config.js
module.exports = {
output: "standalone",
}
node --expose-gc --inspect server.jsthen use chrome devtools to take heap snapshots
ENV NODE_OPTIONS="--max-old-space-size=512"what does your EnvProvider do exactly? if its fetching on every request thats probably the leak |
|
Hi, did you manage to make any progress here? One thing you can quickly try is to turn off image optimization and see what the memory profile looks like during with that, get some data and turn on image optimization again. As pointed out in the other comment, tweaking Rule of thumb (AI suggestion):
A couple of other things are, making sure that you don't hold onto data globally, like when for each request you maybe copy some data into a global store, but never release it. Sometimes there's some Node.js patterns that implicitly do this, and never release memory, but these are less likely. As mentioned above too, Try with next/image, tweak |
|
I'm also curious about progress. I'm encountering what appears to be the same thing. I have disabled image optimization, adjusted max old space size up to 6GB and it continues to grow and hit that limit. I've tried disabling the cache using cacheMaxMemorySize: 0 in config. I've tried limiting it to 128MB. Neither had any impact. I did not get a snapshot of the heap with the 0 setting. I may go back to that and get a snapshot to see if its disregarding that setting. Using Node 22.14.0 and Next 16.1.6. May start messing with versions as well. |
This comment was marked as off-topic.
This comment was marked as off-topic.
|
@icyJoseph this might be related to |
|
We hit what looks like the same thing on 16.2.3 and can add some hard data, because we got as far as heap snapshot retainer analysis. Setup: App Router, custom node server, production build, everything dynamically rendered. Under sustained GET load we retain 0.5-1 MB per request that survives forced GC. Reproduces on Node 22 and on Bun identically, and on 16.3.3 too, so it's not fixed by upgrading. The React.cache suspicion in this thread matches what our snapshots show. Retainer chains of the leaked objects (sampled ~200 promises plus ~400 plain objects via memlab) consistently end in a module-scope nested Map -> WeakMap -> Map structure, which is the cache-node tree shape React's cache() builds, living inside react-server-dom-turbopack-client.node.production.js. It grows monotonically and nothing ever prunes it. In our app the biggest cached content is next-intl (message ASTs + Intl formatters, there's a matching report at amannn/next-intl#2174), plus we see ~225 retained AsyncLocalStorage store entries (kResourceStore symbol) per request. So it looks less like one library's bug and more like per-request cache() scopes or their contents ending up rooted in a module-level cache that never gets released. Current mitigation on our side is an RSS watchdog that gracefully restarts instances before OOM, which obviously isn't a fix. Can provide snapshot analysis output or a repro recipe if a maintainer wants to dig in. |

Uh oh!
There was an error while loading. Please reload this page.
Summary
I'm experiencing a critical memory leak issue with Next.js 16.1.0 that's causing production instability.
Environment:
Next.js version: 16.0.1
Deployment: Docker container running in Kubernetes
Issue Description:
After building and deploying the application, we observe continuous memory growth over several hours, eventually resulting in OOMKilled errors and pod restarts.
Symptoms:
Progressive memory consumption leading to out-of-memory crashes
Linear increase in memory usage over time
Continuous restart cycles affecting service availability
Implementation Details:
Using the await connection() method in the root server component to load environment variables to inject and provide from a EnvProvider
Implemented a /ready GET endpoint in API routes for Kubernetes liveness probes call on every 10s
Request:
Has anyone else encountered similar memory leak behavior in Next.js 16.x? Any guidance on debugging or potential workarounds would be greatly appreciated.
Thank you for your assistance.
Memory Usage:

layout.tsx
EnvProvider.tsx (Thought of use it for env provider for whole application)
package.json

api/ready
Additional information
No response
Example
No response
All reactions