nuxt: Memory leak on server side
Environment
- Operating System:
Darwin - Node Version:
v19.2.0 - Nuxt Version:
3.0.0 - Nitro Version:
1.0.0 - Package Manager:
pnpm@7.18.2 - Builder:
vite - User Config:
- - Runtime Modules:
- - Build Modules:
-
Reproduction
https://stackblitz.com/edit/github-g4zirp-gnp5cq
Describe the bug
- Build the project.
- Run it in “inspect” mode (
pnpm start-debug). - Open Chrome Devtools via
chrome://inspect/#devicesin browser. - In “Memory” tab take heap snapshot.
- Run some load testing tool, f.e. Apache Bench:
ab -n 1000 -c 10 http://127.0.0.1:3000/products/mens
- Then take another snapshot.
- Repeat steps 5, 6 few times.
You will see that each time heap snapshot size increases.
Additional context
No response
Logs
No response
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 32
- Comments: 68 (26 by maintainers)
@pi0 any progress?
This seems to be resolved in the latest Node v20 release: https://github.com/nodejs/undici/pull/2049 and https://github.com/nodejs/node/blob/main/doc/changelogs/CHANGELOG_V20.md.
Thanks for the triaging on this one. We’ll need to look into whether undici + ofetch is causing this issue. (cc: @pi0)
Same for us, with
18.16Node - getting memory leak form 150mb to 24.5Gb 🥹I was experimenting with Memwatch and removing chunks of code and configuration. Removing
asyncDatareduced the amount of time the memory heap fills, but it still steadily climbs to gigabytes of data.I haven’t time to continue debugging Today.
My next stage is to get a fresh install of nuxt.js and double-confirm the memory heap usage over time with excessive requests to the server locally.
I’m unsure where to debug in the core nuxt js application for this heap issue.
Feel free to continue sharing your findings.
I have an instance of munching through memory at an unhealthy rate.
node
20.11.0nuxt2.17.3Our issue is not fixed with node 18.17.0 or with the latest node 20.
The undici 5.21.0, included in Node.js 18.16.0, seems to be leaking memory. https://github.com/nodejs/node/issues/46435
If you are using node-fetch-native, you may be able to work around this by setting the FORCE_NODE_FETCH environment variable.
@danielroe @pi0 the issue was narrowed down to the memory leak from node 18.12 -> 18.13 to be the use of useLazyFetch with an external source. This seems to be a HUGE leak in 18.13
We’ve set up a project with a couple of examples and steps to reproduce. https://github.com/nick11703/nuxt-3-memory-leak please, have a look
@soylomass for me the issue was related to this: https://github.com/vueuse/vueuse/issues/3498
I accidentally registered event listener in script setup and forgot to unregister these listeners causing the memory leak. Details and fix is in the linked issue. Keep in mind that I had two different code bases. One used vueuse eventBus and the other used mitt for the event bus. Both registered event listeners in script setup, both had a memory leak and both were fixed by moving the register to onMounted and adding an unregister in onBeforeUnmount.
Maybe that helps someone here. 😃
@kylewelsby I’m still investigating this leak thanks.
These are the common dependecies that we have:
“nuxt”: “2.17.3”, “nuxt-jsonld”: “^1.4.10”, “@nuxtjs/tailwindcss”: “^6.11.4”, “@nuxtjs/axios”: “^5.13.6”, “@nuxtjs/i18n”: “^7.2.3”,
The problem happens only in production on local with --inspect I couldn’t see anything about a memory leak:
Node: 21.7.1 Nuxt 2.17.3 yarn Docker
I have tried with all major versions of node: 16, 18, 19, 20 and 21 in all of them I have the leak. If I find something I will post here to update.
@PoliPH unfortunately not. I tried upgrading my node versions and it still occurs.
Node:
20.11.0Next2.17.3I have tried removing as many NPM packages as my project currently can handle.
https://github.com/kylewelsby/v4.soulectiontracklists.com/blob/197fda7809f35b1daf78c6ae1245923905d8c9c8/package.json
Even tried increasing the memory amount, but the instance still steadily eats through the memory before crashing after a few days.
Well, it’s almost certainly a bug in that version of Node. It would be worth checking if there’s an issue already raised.
@danielroe I’ve added a new issue for my memory leak, not to clutter here anymore. https://github.com/nuxt/nuxt/issues/23193
Unfortunately it seemed to work on my development environment, but the issue remains on the staging environment. There the application is run inside a docker container. I’ll do some more tests, and try to reproduce this in stackblitz now.
@danielroe @konradsienkowski From my first tests, the memory leak issue seems to be solved in 3.7.1 (just updated to last version).
API calls on the error page should still happen. If you can raise a minimal reproduction and a new issue we’ll investigate.
I think it has been fixed because it was updated to undici 5.22.1 in Node.js 18.17.0.
@danielroe Just tested
18.16.0- memory leak is there 😐Thank you for continuing to investigate. From this, it seems it is likely an upstream bug in the native Node fetch implementation (now fixed) as we previously guessed.
I wouldn’t be suprised if there’s also a fix merged in latest Node 18 - it would be worth confirming that too.
In order to test this correctly, you need to garbage collect before taking the snapshot as node is very greedy with memory and will not release it unless it absolutely has to. When I do that I do not see any memory leak.