Next.js Discord

Discord Forum

Please Help !! Memory leak or not in Next 13.4.12 project

Unanswered
Dwarf Hotot posted this in #help-forum
Open in Discord
Dwarf HototOP
VPS OS : Debian 11.2 (CT)
CPU : 4 cores
Memory : 4GB
Storage : 1TB

I have a Next13.4.12 project. I have used it to build a affiliate site where I am fetching large amount of movie details and pictures from a third party api. i am using API routes, mongodb and also using Next Image as well for Image optimization. I have deployed the project in a VPS server. The server ran without any issues for 2 days and suddenly now I cannot access my VPS server. When I asked my service provider, However part of the site is working like displaying images and movie details from the third party. But I cannot see features which uses useSWR hook based implementations in client components (Notice banners).

He said that:

"They have checked the server status and it appears that the target server has reached its thread limit. If you are running a program that starts many processes, please reboot the server and adjust the number of threads and processes if possible. If the thread limit has not been reached, we believe that the web console can be displayed."

But the thing is I cannot even restart the server from VPS dashboard. So I wanted to check for memory leaks by running the website in my local machine with production build. I checked for below snapshots. Can anyone please help me whether the issues is a memory leak or something else? I am really greatful for that help. please I have been struggling for 5 days now and now i am lost to find the reason for this issue.

FYI: Even though I have installed sharp, when running the production build, it still gives the missing sharp package warning. But I have even included the NEXT_SHARP_PATH=/tmp/node_modules/sharp env variable as well.

49 Replies

Dwarf HototOP
VPS server usage stats
@Dwarf Hotot VPS server usage stats
Forest bachac
are you saving stuff in memory for a while for any reason?
not cleaning sutff up
you might not have memory leaks your site might just take a lot of memory bc as you said your fetching large amounts of data
@Forest bachac not cleaning sutff up
Dwarf HototOP
I am using a embla carousel to have a horizontal slider and I load maximumly around 15 images per one slider. There are 3 sliders. Apart from that in product search page I load maximumly 75 image components to view products. Also I have turned off prefetch in all <Link> components https://www.embla-carousel.com/api/
Also there are like 3rd party iframes (around 5) which loads affiliate ads which contains lot of images. But they are loading through the iframes
Forest bachac
do you get memory leaks if you dont use embla carousel?
comment it out and see the memeory usage
try upgrading next to latest too
@Forest bachac comment it out and see the memeory usage
Dwarf HototOP
is there a good alternative for Embla? by the do you know what my service provider meant by thread limit has passed. I am somewhat unclear on that. Thanks
Forest bachac
tbh idfk what embla is
@Forest bachac tbh idfk what embla is
Dwarf HototOP
It's a image slider component
Forest bachac
do you get the memory leak without using it/temporarily just not have image sliders
wait are you using async for backend operations
@Forest bachac wait are you using async for backend operations
Dwarf HototOP
Yes i am using async
Forest bachac
i thought it was a memory thing but too many threads could just mean your not handling incoming requests efficiently
have you tried deploying on vercel might be better for next
@Forest bachac i thought it was a memory thing but too many threads could just mean your not handling incoming requests efficiently
Dwarf HototOP
can you please elaborate this part. I really appreciate it/
Forest bachac
Most likely your site uses too many threads/memory/cpu that you need to upgrade for more @Dwarf Hotot
Unless your doing something inefficientlly/not cleaning stuff up
@Dwarf Hotot VPS server usage stats
Forest bachac
based on this you arent using anywhere near max cpu or memory
can i ask what service provider ur using?
Dwarf HototOP
it's actually a simple affiliate site with mainly 3 pages. by the way I observed that still operations like directly fetching from the 3rd paty api functions (util function) are working fine in the live server. But all API route based operations are down. Nothing is working but the website is still live. But I cannot even ssh to the server. the ports are giving a timeout error
operations with the database is down
@Forest bachac can i ask what service provider ur using?
Dwarf HototOP
Yes sure. it's a japanese service provider. called GMo cloud

https://gmocloud.com/

I am using thier VPS service
European sprat
Step 1 to troubleshoot this is to see what the server logs are showing
Is there a big slew of errors being thrown? Is there any other hints in the logs?
Dwarf HototOP
@European sprat Is there a big slew of errors being thrown? Is there any other hints in the logs?
Dwarf HototOP
Unfortunately I cannot access the server logs
European sprat
Well if I couldn't see logs where I was hosting something I'd be changing hosting providers immediately
@Dwarf Hotot Unfortunately I cannot access the server logs
Dwarf HototOP
Actually normally through SSH or web console I can check the server logs. I use PM2 but didn't setup remote monitoring feature. So now I cannot access to the inside of the server and I am kind of blocked. Only solution is to recreate the serve instance and re do everyting
Service provider said that because of thread limit passed, i won't be able to ssh into the server
European sprat
Reboot the server?
@European sprat Reboot the server?
Dwarf HototOP
It is also prohited. I am getting an error. Only thing I can do is delete the server instance and recreate it.
Hi is there a way for me to debug to check if I used over multiple threads in my website. through stress test or something?
Forest bachac
try it
if the site, as you say, doesnt need that many resources see if it works without issues on a free vercel acc @Dwarf Hotot
Dwarf HototOP
i am really dissapointed
on NextJs
European sprat
did you reboot the server or recreate the server and check logs?
@Dwarf Hotot i am really dissapointed
Forest bachac
Pretty sure its your server host not next
Looking at your logs it looks like you were at barely 30% usage for cpu and ram
Dwarf HototOP
Hi @Forest bachac @European sprat I think I found the issue. I am using a self hosted mongodb server inside my VPS server and I am using Next13 api routes to communicate with the database. There's no seprate backend server. basically If I'm not wrong I am using Nextjs13 serverless functions to communiacte with my locally hosted mongodb satabase, When I investigate the mongod service , I observed that the thread count consume by mogodb increases when I access the site through a client browser. But when I close the browser, mongod service doesn't free the thread, instead it add more threads. I checked again by doing multiple request to the site and observed that mongod thread count get gradullay increase. So when I close all those browser tabs, the thread count get double.

I was actually able to investigate this by recreating the environment in my Mac.
is there a way I can solve this issue. There are only 2 small colelctions in my DB. One for admin user authentication and other collection is barely having only 3 documents which have deatils for site noticeses and banners, email address for inquiries
Dwarf HototOP
Hi I was able to fix it. Issue was with my mongodb config file and the driver version.
I love NextJs. Sorry for accusing it
European sprat
Glad you figured it out
Forest bachac
👍