Replies: 3 comments 5 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 |
Beta Was this translation helpful? Give feedback.
-
|
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 |
Beta Was this translation helpful? Give feedback.
-
|
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. |
Beta Was this translation helpful? Give feedback.
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
Beta Was this translation helpful? Give feedback.
All reactions