-
Notifications
You must be signed in to change notification settings - Fork 27k
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 and Segmentation Fault with nextjs-blog Official Example #32526
Comments
Seems to be related to issues: |
Just had a look into this, using version 12.0.8 I'm unable to reproduce a memory leak with the steps provided as the heap usage is consistently around 70MB at the most. I've even tried running it with a really low heap size The eventemitter warning is triggered because there's more than 10 |
Hi Tim, thanks for looking into this! Just to doublecheck, did you repeatedly create heap snapshots? What I noticed was although the memory size stays consistent every time I take a snapshot, after taking a few the segmentation fault would occur despite all previous snapshots having the same memory. My uneducated guess would be that the memory usage does not consistently increase in a linear fashion; something triggers it and from there it increases until segmentation fault. There seems to be a bit of stochasticity in when it occurs as sometimes I will need to take 10+ snapshots and sometimes the segmentation fault occurs after only 3. This also seems to parallel the memory issue I'm facing with my production app hosted on Azure: the memory usage will be constant for a couple of days and suddenly will start increasing until a segmentation fault occurs, despite no one accessing the app. |
This issue has been automatically closed because it received no activity for a month and had no reproduction to investigate. If you think this was closed by accident, please leave a comment. If you are running into a similar issue, please open a new issue with a reproduction. Thank you. |
Not sure if this helps anyone, but the memory leak has disappeared since we have implemented "Application Insights" to our code following https://docs.microsoft.com/en-us/azure/azure-monitor/app/nodejs#get-started. My suspicion is that the querying of the app externally to obtain insights causes the memory leak, but if the insights are generated from within the code, then this is circumvented. |
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
What version of Next.js are you using?
latest
What version of Node.js are you using?
14.18.1
What browser are you using?
Chrome
What operating system are you using?
Linux (WSL2 on Windows 10)
How are you deploying your application?
For my production code: Azure WebApps
For the same issue in this minimal example: Locally
Describe the Bug
On my production code in Azure, I have recently experienced a memory leak and consequent segmentation fault. Below is the monitored behaviour where notice that the segmentation fault occurs once at around November 30th and another time at around December 11th:
Below the log of the error:
In particular, notice
2021-12-11T01:20:33.944804366Z FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
.To investigate, I followed the instructions in https://alberic.trancart.net/2020/05/how-fixed-first-memory-leak-nextjs-nodejs/ to recreate it locally and was successful with my production app. To further isolate whether the problem was in my code, I attempted to create the same issue in minimal Nextjs projects, eventually arriving at the default project detailed in https://nextjs.org/learn/basics/create-nextjs-app/setup and was successful in creating a segmentation fault. Therefore, I'm wondering if the behaviour described below should occur at all and, if not, I'm hoping the required fix will also fix my production app.
The issue is as follows: after using
npm run dev
to run the development environment on my localhost, using Chrome DevTools to inspect and taking heap snapshots whilst loading the server with 25 requests per second, I eventually managed to induce a segmentation fault:Notice in particular the
I should mention though that the heap snapshots do not seem to indicate an increase in memory before the segmentation fault occurs:
where here, the last snapshot stalls on 93% because of the segmentation fault. For larger Nextjs projects, this segmentation fault occurs much earlier; sometimes even unable to complete the first snapshot after loading the requests. Also, although the EventEmitter memory leak warning appears immediately upon loading the local server with requests, the segmentation fault does not appear unless repeated heap snapshots are taken.
I should probably also mention that I performed the same experiment with a vanilla React app and was unable to reproduce the segmentation fault.
Expected Behavior
No memory leak warning should appear nor should a segmentation fault
To Reproduce
These steps follow https://alberic.trancart.net/2020/05/how-fixed-first-memory-leak-nextjs-nodejs/
npx create-next-app nextjs-blog --use-npm --example "https://github.com/vercel/next-learn/tree/master/basics/learn-starter"
"dev": "NODE_OPTIONS='--inspect' next dev"
npm run dev
./load.sh 25 "http://localhost:3000/"
to send 25 requests per second. Observe the appearance of the warning:The text was updated successfully, but these errors were encountered: