Next.js: Nextjs custom server typescript memory leak...

Created on 8 Feb 2020  路  22Comments  路  Source: vercel/next.js

Bug report

Describe the bug

memory leak.
On my server i have rest api that handles requests coming from getInitialProps.
The server sometimes uses apollo-fetch to get data from other api.
emotion js is used for styling.
That's mainly what the app does.
I am running another service with nodejs and have no issues at all.
I followed the examples but couldn't fix the memory leak.
While inspecting the memory leak, I could only see webpack strings growing.

To Reproduce

Create custom server with typescript.
Use apollo fetch
Use emotion
Use mongoose
Use lru-cache

...

Expected behavior

Stable service.

Screenshots

image

System information

google cloud kubernetes

please add a complete reproduction

Most helpful comment

Countless reports that next.js has memory leaks and all issues are closed without resolve.
Screen Shot 2020-03-04 at 0 03 57
Screen Shot 2020-03-04 at 0 32 59

All 22 comments

This issue is not actionable from our side as no reproducible demo is provided.

This issue is not actionable from our side as no reproducible demo is provided.

I have no idea how to add complete reproduction . Shouldn鈥檛 there be documentation on how to avoid memory leaks on custom server ? Or cases?

I facing this situation either, using ts run a custom server, after 10 days or more, memory leak, cpu peaks, finally system broken.

I think that this is clear that, nextjs doesn't have clear guidelines on how to avoid memory leaks and do and don'ts.
It is not a normal nodejs application and it has to have specific guidelines on how to develop without memory leaks.

Could it be just the growing size of lru-cache rather than the memory leak? What if your storage settings are just a bit too generous? One way or the other, I鈥檓 not sure how the maintainers can help given a rather vague problem definition and lack of reproduction example.

Debugging memory leaks in Next.js servers is no different to debugging leaks in Express or a plain node process. I鈥檓 not sure what guidance specific to Next.js could be written down.

Closing as low activity for a week. There is no special way to troubleshoot memory issues in a Next.js app versus normal Node apps.

Could it be just the growing size of lru-cache rather than the memory leak? What if your storage settings are just a bit too generous? One way or the other, I鈥檓 not sure how the maintainers can help given a rather vague problem definition and lack of reproduction example.

Debugging memory leaks in Next.js servers is no different to debugging leaks in Express or a plain node process. I鈥檓 not sure what guidance specific to Next.js could be written down.

I have tried to manage everything related to lru-cache but the app is still increasing in memory usage over time.
I seriously don't know what to do. Next.js doesn't offer any guidelines to avoid memory leaks.
It's an SSR react application there is lots of holes for memory leaks.
I am running my app in production for more then a year now.
I have 2k visitors per day, more than 20k pages. and around 40k requests per day.

Please share some documentation on what things can cause memory leaks in SSR nextjs application.
from styling, webpack,babel, to caching.
Screen Shot 2020-02-26 at 13 53 33

It might have been the cache. Nice drop finally happened after few hours.
Screen Shot 2020-02-27 at 3 18 06

Countless reports that next.js has memory leaks and all issues are closed without resolve.
Screen Shot 2020-03-04 at 0 03 57
Screen Shot 2020-03-04 at 0 32 59

I have the exact same problem, using azure and I get the same memory behaviour with a same type of graph you're showing. @RayLuxembourg did you end up resolving this issue?

tried to disable lru-cache but issue remains


Screenshot 2020-04-22 08 39 13

This should be fixed in the latest version of Next.js. What version are you on?

This should be fixed in the latest version of Next.js. What version are you on?

great news! I'm on version 9.2.1

Please upgrade to 9.3.5 or newer and let us know!

@Timer Unfortunately reporting that the latest version didn't fix my memory leak issue. What has changed in that version that made you guys think this would be addressed? perhaps I overlooked something, my custom server is pretty simple though (https://github.com/madeinspace/EcoNext/blob/master/server.js)

Hello! Having the same issue than @madeinspace. Same type of memory behavior with Next on Azure. Updated to 9.3.5 but didn't fix it.
I will try to reproduce this in my local env but any reference to other issues might help.

I'm using styled-components and I'm reading there are some potential issues with this too, @AlbericTrancart are you using SC too?

Hello,
Having the same issue. Updated to 9.3.6 but didn't fix it. We are also using typescript and styled component.

Hello haveing the same issue. @RayLuxembourg Have you figured out the issue?

@elliottcrif @RayLuxembourg did you guys try to update 9.5.x ? I had rarely seen out of memory problems in previous versions, but with 9.5.x it happens very often. So i am wondering if its also much worse for you on 9.5.x ?

Hello I have the same issue, it is now fixed ?

@macrozone I have been experiencing FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory server crashes in development at an increasing frequency, after updating to 9.5.x

It's at the point where it's severely impacting DX, needing to manually restart the app server every 5 minutes.

Adding NODE_OPTIONS=\"--max-old-space-size=4096\" in front of my start script seems to help for now..

They probably dont want to help resolve this as they want you to use Vercel servers.

@timdavish im facing the same issues using next version 9.5.1 and I did a similar thing. I am using pm2 to manage my node server

I create this file pm2.json and then you can run pm2 start pm2.json

{
  "apps": [
      {
          "name": "profiles",
          "script": "server.js",
      "max_memory_restart": "1800M",
      "node_args": "--max_old_space_size=2048"
      }
  ]
}

with that configuration pm2 automatically restarts the application when it reaches 1.8g of memory so it never reaches the server maximum, problem is that because when that happens the server looses all the cache of generated files then first time a user visits a page, it takes a lot of time to re-compile.

Was this page helpful?
0 / 5 - 0 ratings