-
Notifications
You must be signed in to change notification settings - Fork 13
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
[BUG] applicationinsights-web is leaking memory in NextJS SSR #59
Comments
Ok, I've not been able to repo your issue so far, because the repo steps above don't work for the min example
But looking at the heap dumps and the pattern that is occurring, it seems to me that on the server side it is creating a "new" Application Insights for every request, which is initializing (and never unloading), so every request we keep creating new instances as there are "lots" of importModuleDynamically. If this is
We support (and need to support) having multiple instances loaded and running in a single runtime (usually a browser) as each instance (is normally) for a different component ( Still assuming that my assumption (about the multiple instances being initialized) is correct you have 2 options
Still assuming this is the root issue, I would suggest that you implement a combination of the above
Note: I'm not a Next.Js developer and I've not used it for a server side "node" implementation. The above is derived (and assumed) from my previous history with other server side environments / runtimes (mostly Java, C++ and .Net -- at massive scales) |
Can you either please provide a repo for your simple example, or confirm that the above assumption is likely the issue? |
Thank you @MSNev for investigation. I'm will answer all your questions, but let me provide instructions for min repo:
But make sure you have clinic js installed globally. |
yes, this is probably the case. I'm not nextJS internals expert. But I believe AppInsights context - https://github.com/mauron85/react-appinsights/blob/main/pages/_app.tsx#L7 is initializing for every request creating new instance of reactPlugin. I understand your recommendations about creating shared pool or unload, however not sure if there is any request lifecycle method in NextJS I can hook into. At least not officially in their docs. |
I put console log into components\ApplicationInsightsService.ts to detect if NextJS is creating new instances of ApplicationInsights and ReactPlugin for every request. But it is not. It is only called once, meaning there is only single instance of both. |
Hmm, I'm not sure whats going on there as this definitely "looks" like a multiple instance situation. You could try adding (If) this stops the memory from consistently increasing then that "would" suggest that it is creating / loading multiple instances.... Then "perhaps" the location with the console.log is not in the place that is getting duplicated or is the runtime doing some optimization where if the same message is logged it just "increases" a count of ignores the value??? I know in the latest browsers the console (in F12 dev tools) only lists repeated messages once but has a "tag" saying the number of repeated times that line is displayed -- Stabbing in the dark on this. |
The console.log is for sure not deduplicated. It's actually writes to windows cmd/terminal (since it's running in nodejs process due SSR). I did change EDIT: still leaking with config, but mem profile is bit different
|
@siyuniu-ms can you please try and to reproduce this locally |
@siyuniu-ms what node version are you using? Please try with Node 18.17.1 (or later). Maybe you are experiencing this bug: clinicjs/node-clinic#430 |
Thanks. That helps work. |
investigate ongoing...... |
Hi @mauron85 ! I've replicated the issue at the doctor clinic, and it seems to yield the same result as the image you provided. However, since the tool used there doesn't offer detailed information about memory usage, it's proving challenging to conduct a deeper investigation. Could you please share the method you used to generate the screenshot (https://user-images.githubusercontent.com/359450/273190291-3d9f7a94-773f-44fe-9b68-96c017040d40.png)? Additionally, I'd greatly appreciate it if you could provide instructions on how to generate Heap Snapshots for analysis in Chrome using the testapp. I attempted to ping/call track locally after running the server, but I couldn't spot any requests using the Chrome DevTools. |
The image was captured from app live production app on appservice monitoring site. So I assume this is not an option for you. Regarding chrome snapshots. You can:
And connect to debugging instance of nodejs from chrome by visiting chrome://inspect - and then clicking link "Open dedicated devtools for Node" |
Hi @mauron85, my apologies for the delayed response. It took us a considerable amount of time to replicate the issue, but during my local testing, I didn't observe any memory leaks. Here's a summary of the steps I took:
|
may related to microsoft/ApplicationInsights-JS#2311 |
I believe that this issue is now addressed by the above fix from |
Description/Screenshot
Steps to Reproduce
@microsoft/applicationinsights-react-js: 17.0.1
@microsoft/applicationinsights-web: 3.0.3
Heap Snapshots for analysis in Chrome:
heapsnaps.zip
Min repo:
https://github.com/mauron85/react-appinsights
Install clinic.js and autocannon for testing:
Build example app:
test load:
Autocannon will run 100 concurrent connections for 10 min.
After 10min CTRL+C to to stop server and clinicjs will generate mem usage report.
Expected behavior
Memory is not leaking
Additional context
App is constantly leaking memory on server side NextJS server. After removal of appinsights, memory consumption is flat.
In real world app, effect is applified greatly.
Tasks
The text was updated successfully, but these errors were encountered: