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

Heap memory Leak | each PDF generation increase memory and does not fall back enough. #2848

Closed
kristinaright opened this issue Aug 22, 2024 · 6 comments

Comments

@kristinaright
Copy link

kristinaright commented Aug 22, 2024

First of all, thank you for such complicated tool! Awesome work!

Describe the bug
During performance testing we noticed memory leak. I tried to isolate the problem and could not found any reason other than using render function from react-pdf/renderer lib.
Every time a PDF is rendered, the memory used by the app increases and never comes back down to what it was before the rendering occurred. Garbage collector works but it seems like not enough objects deleted, seems like some objects still in use after generation finished.
Also i noticed that if we trying to get a lot of PDFs one after another fast - we will get yoga error and i do believe this problems are related(MaxListenersExceededWarning: Possible EventEmitter memory leak detected in yoga-layout)

PS. i saw previously closed issues but i did not found working solution and issue still exists for us in production.

To Reproduce
Steps to reproduce the behavior including code snippet (if applies):

  1. Clone repo https://github.com/kristinaright/react-pdf-example/tree/develop
  2. Go to /service and run dev script
  3. Open chrome://inspect/#devices
  4. Send GET request localhost:3000/documents/basic and watch for memory usage

Expected behavior
Memory should fall back in some time after PDF generation.

Screenshots
Снимок экрана 2024-08-22 в 17 34 29
Снимок экрана 2024-08-22 в 17 36 42

Desktop (please complete the following information):

  • OS: MacOS, Linux
  • @react-pdf/renderer: "^3.4.2"

Thank you in advance!

@jaidetree
Copy link

jaidetree commented Aug 23, 2024

Experiencing this issue as well in a production app. A co-worker of mine reached the same conclusion after tracking it down for 2 weeks. At least this double confirms it 😁

@kristinaright
Copy link
Author

Experiencing this issue as well in a production app. A co-worker of mine reached the same conclusion after tracking it down for 2 weeks. At least this double confirms it 😁

Hello @jaidetree
We spent some time to investigate a fix for this issue with my colleague and looks like we found a trick which solving it.
It seems like yoga-layout lib does not remove some listeners so we tried to clean its ourselves and it helped.

Take into account that our app - simple Node.js app which provides an API endpoints to get data and render PDFs based on templates - so in our case its not a huge trick to call process.removeAllListeners() right after PDF generation finished.

On real project you can try to add next line here:
finally { process.removeAllListeners(); }

https://github.com/kristinaright/react-pdf-example/blob/develop/service/src/server/respondWithPdf.tsx#L12

We going to run perf tests later to doublecheck but as for now - it works fine.

Hope it may help you :)

@jaidetree
Copy link

Experiencing this issue as well in a production app. A co-worker of mine reached the same conclusion after tracking it down for 2 weeks. At least this double confirms it 😁

Hello @jaidetree

We spent some time to investigate a fix for this issue with my colleague and looks like we found a trick which solving it.

It seems like yoga-layout lib does not remove some listeners so we tried to clean its ourselves and it helped.

Take into account that our app - simple Node.js app which provides an API endpoints to get data and render PDFs based on templates - so in our case its not a huge trick to call process.removeAllListeners() right after PDF generation finished.

On real project you can try to add next line here:

`finally {

process.removeAllListeners();

}`

https://github.com/kristinaright/react-pdf-example/blob/develop/service/src/server/respondWithPdf.tsx#L12

We going to run perf tests later to doublecheck but as for now - it works fine.

Hope it may help you :)

Thanks that's immensely helpful!

@diegomura
Copy link
Owner

Should be fixed by the latest yoga layout changes. Please try it out and re-open if this is not the case!

@jaidetree
Copy link

Should be fixed by the latest yoga layout changes. Please try it out and re-open if this is not the case!

Confirmed the fix is working on our end at least. Thanks!

@kristinaright
Copy link
Author

Should be fixed by the latest yoga layout changes. Please try it out and re-open if this is not the case!

Thank you for fast reaction!
Updated version works for us too!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants