Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak? #9427

Closed
kevinrenskers opened this issue Mar 15, 2023 · 34 comments · Fixed by #9740 or #9761
Closed

Memory leak? #9427

kevinrenskers opened this issue Mar 15, 2023 · 34 comments · Fixed by #9740 or #9761
Labels
bug Something isn't working
Milestone

Comments

@kevinrenskers
Copy link

kevinrenskers commented Mar 15, 2023

Describe the bug

I am seeing the memory usage of my SvelteKit app, running with the Node adapter, going up and up. I'm not sure how the server's memory usage can keep increasing, all the svelte stores for example are only used in the browser, and other than that the server requests should be stateless, right?

Screenshot_2023-03-05_at_11 47 49

Screenshot 2023-03-16 at 09 12 14

See https://discord.com/channels/457912077277855764/1081891569390583898 for a discussion about this.

Reproduction

As far as I know, any project will show this problem.

Create a new skeleton project. No need to add TypeScript, Prettier, ESLint or anything else.

npm create svelte@latest repro
cd repro
npm i
npm run build
npm run preview

Open http://127.0.0.1:4173/, open the activity monitor (on macOS) or top or whatever else to see memory usage of the Node process, and refresh the webpage a whole bunch of times. You'll see the memory go up. It's slow to increase with such a simple skeleton project so you might need to refresh a whole bunch of times, but it does go up.

In my moderately popular website it goes up by about 300 MB per day.

Logs

No response

System Info

System:
    OS: macOS 13.2.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 274.47 MB / 32.00 GB
    Shell: 3.6.0 - /opt/homebrew/bin/fish
  Binaries:
    Node: 16.17.0 - ~/Library/pnpm/node
    Yarn: 1.22.19 - /opt/homebrew/bin/yarn
    npm: 8.15.0 - ~/Library/pnpm/npm
  Browsers:
    Chrome: 111.0.5563.64
    Edge: 110.0.1587.69
    Firefox: 110.0.1
    Safari: 16.3
  npmPackages:
    @sveltejs/adapter-auto: ^2.0.0 => 2.0.0
    @sveltejs/kit: ^1.5.0 => 1.11.0
    svelte: ^3.54.0 => 3.56.0
    vite: ^4.0.0 => 4.1.4

Severity

serious, but I can work around it

Additional Information

I have to restart my Node server once in a while or the server just runs out of memory.

@Rich-Harris Rich-Harris added the bug Something isn't working label Mar 17, 2023
@Rich-Harris Rich-Harris added this to the soon milestone Mar 17, 2023
@vhscom
Copy link

vhscom commented Mar 24, 2023

@kevinrenskers this issue appears to be related to Undici, which is used under the hood for the fetch implementation in Node. As a workaround, you may be able to swap out SvelteKit fetch with Axios until this bug is resolved.

@kevinrenskers
Copy link
Author

kevinrenskers commented Mar 24, 2023

Is that being tracked in their repo somewhere?

Switching to Axios is not really an option, I don't have time to go through the entire app to make such a refactor.

@eltigerchino
Copy link
Member

eltigerchino commented Apr 11, 2023

This should be closed by #9591 where we pin undici's version.
@kevinrenskers can you confirm if this issue is still present for you after upgrading to @sveltejs/[email protected] ?

@kevinrenskers
Copy link
Author

kevinrenskers commented Apr 11, 2023

I've deployed a new version, will keep an eye on the memory usage and report back.

This was the last 30 days, for reference:

Screenshot 2023-04-11 at 16 47 21

@kevinrenskers
Copy link
Author

kevinrenskers commented Apr 11, 2023

I don't think that helped, it keeps increasing in basically the same speed as before; 30 MB in 2 hours. Which doesn't sound like a huge amount, but is 360 MB per day.

Screenshot 2023-04-11 at 18 46 07

Update:

It just keeps on rising, no change.

Screenshot 2023-04-11 at 21 39 36

Is nobody else seeing this? Or is nobody else tracking their server stats?

Update 2:

Screenshot 2023-04-12 at 13 53 16

🤷‍♂️

@machycek
Copy link

@kevinrenskers we are having the same issue actually

@gtm-nayan
Copy link
Contributor

@kevinrenskers
Was this using the default server that's provided by the adapter or using just the handler in a custom server?

@kevinrenskers
Copy link
Author

The default server, using adapter-node.

@gtm-nayan
Copy link
Contributor

And this also happens with npm run preview?

@kevinrenskers
Copy link
Author

I don't know, I don't want to run my production website that way to find out 😅 I'll try it locally, see what happens.

@dominikg
Copy link
Member

can you please confirm the exact versions of operating system, node, @sveltejs/kit, @sveltejs/adapter-node and undici where you are observing this behavior?

@kevinrenskers
Copy link
Author

kevinrenskers commented Apr 15, 2023

can you please confirm the exact versions of operating system, node, @sveltejs/kit, @sveltejs/adapter-node and undici where you are observing this behavior?

  System:
    OS: Linux 5.10 Debian GNU/Linux 11 (bullseye) 11 (bullseye)
    CPU: (4) x64 AMD EPYC Processor
    Memory: 4.68 GB / 7.58 GB
    Container: Yes
    Shell: 5.1.4 - /bin/bash
  Binaries:
    Node: 16.19.1 - /usr/bin/node
    npm: 8.19.3 - /usr/bin/npm
  npmPackages:
    @sveltejs/adapter-node: ^1.0.0 => 1.2.3
    @sveltejs/kit: ^1.0.0 => 1.15.4
    svelte: ^3.54.0 => 3.58.0

undici: 5.20.0

@kevinrenskers
Copy link
Author

And this also happens with npm run preview?

Yes

@stalkerg
Copy link
Contributor

Can you try "@sveltejs/adapter-node": "1.2.2", and "undici": "5.20.0" and also do this:

"overrides": {
		"@sveltejs/kit": {
			"undici": "$undici"
		},
		"@sveltejs/adapter-node": {
			"undici": "$undici"
		}
	}

I have no issue now. Also, undici devs fixed such a memory leak (very tricky way actually!).

@kevinrenskers
Copy link
Author

I'm already using undici 5.20.0 though, but sure I'll try. Where do I put these overrides?

@stalkerg
Copy link
Contributor

on the same level as "devDependencies": { also, very important to downgrade adapter-node as well (undici bundling by bable)

@kevinrenskers
Copy link
Author

kevinrenskers commented Apr 19, 2023

So far it seems improved, but I'll know for sure in a day or so. I'll report back.

Update: after 4 hours memory usage has increased by about 15 MB. Let's see what's it like in 24 hours.

@kevinrenskers
Copy link
Author

I think the leak is indeed solved 🎉
At first the memory increased by about 15 MB, but it's been stable at ~60 MB for about 12 hours now. Huge difference.

@dummdidumm
Copy link
Member

@stalkerg am I seing this correctly that undici has released the fix for the memory leak in https://github.com/nodejs/undici/releases/tag/v5.21.1 so we can bump to latest undici again?

@stalkerg
Copy link
Contributor

@dummdidumm yes! Now it's fixed without a timeout breakage. My small local tests are working well.

@kevinrenskers
Copy link
Author

kevinrenskers commented Apr 24, 2023

This fix isn't released yet, right? Because I updated all my dependencies, removed the overrides, and the problem is back in full force.

You can clearly see when I deployed the changes 😅

Screenshot 2023-04-24 at 19 13 18

@eltigerchino
Copy link
Member

eltigerchino commented Apr 24, 2023

This fix isn't released yet, right? Because I updated all my dependencies, removed the overrides, and the problem is back in full force.

No, it has not released yet.

@kevinrenskers
Copy link
Author

Is there an ETA on a release? So I can decide if I should revert my change or just wait a bit.

@eltigerchino
Copy link
Member

Is there an ETA on a release? So I can decide if I should revert my change or just wait a bit.

It's out now! :) https://github.com/sveltejs/kit/releases/tag/%40sveltejs%2Fkit%401.15.8

@stalkerg
Copy link
Contributor

@s3812497 it's not enough; we also must update the node adapter because of bundling (this issue is not in direct dependencies)

@eltigerchino
Copy link
Member

@s3812497 it's not enough; we also must update the node adapter because of bundling (this issue is not in direct dependencies)

Ah, sorry about that. I didn't know.

@stalkerg
Copy link
Contributor

@s3812497 @sveltejs/adapter-node/files/shims.js still includes old undici and the node adapter will use it.

@stalkerg
Copy link
Contributor

@dummdidumm we must update adapter-node as well, just sveltekit is not enough.

@itssumitrai
Copy link

Can we fix this asap ? Our production server capacity is lost by more than 100%. Basically we need to have more than twice the servers and the perf has become really bad as well. We have no choice other than to pin down adapter node. Can this issue get more traction ?

javiersanp added a commit to OSM-es/visor-catastro that referenced this issue May 15, 2023
sveltejs/kit#8239
sveltejs/kit#9427
Hay que vigilar si se resuelve el problema de undici
@henryson
Copy link

About 1.5 years later I experience this same problem. I thought this thread would help me but since I run svelte-kit 2.5.4 I guess the above fix should already be there. Maybe a similar problem has arised again? Or should I continue look elsewhere?

@stalkerg
Copy link
Contributor

@henryson did you use latest stable nodejs? Also, are you sure you have totally same issue? You can go to undici tracker than.

@henryson
Copy link

henryson commented Nov 29, 2024 via email

@stalkerg
Copy link
Contributor

You should isolate your issue and make small reproduction demo. Without it, any suggest will not too different from random advice.

@dominikg
Copy link
Member

this specific issue has been resolved. it makes no sense to discuss all possible memory leaks in the comments here and notify participants in the process. - locking it now, please file a new one with a reproduction and downloadable heap snapshots for comparison that show it is actually sveltekit causing the memory leak.

If you are unsure how to do this, please join https://svelte.dev/chat and ask for help in #help-svelte-and-kit

@sveltejs sveltejs locked as off-topic and limited conversation to collaborators Nov 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.