react-pdf: Memory leak - Every time a PDF is generated, the memory used increases and never comes back down.

First, Thank you for the awesome work on this library!

Describe the bug We notice that rendering PDFs caused a memory leak in our prod environment. We dedicated some work to isulating the issue and it seems to be inside the react-pdf, because it only happens when we call it. There’s a chance I could be wrong but I havent been able to find anything else that causes this besides us calling the react-pdf library. While running react-pdf in a node server, 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.

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

  1. watch the memory used by your application that uses react-pdf.
  2. Notice how the memory used by the app increases when a PDF is rendered. and it never comes back down.

You can notice the difference better once you render over 50 or 75 documents.

HERE is a quick and easy way to reproduce it.

clone this project: https://github.com/Osuriel/react-pdf-test

the readme.me has all the steps to run it and easily monitor it.

Expected behavior The memory should eventually go back to normal after a request is made and not increase continually.

Screenshots before rendering pdfs: image

after rendering 100 pdfs, and waiting for a while: image

Desktop (please complete the following information):

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

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 9
  • Comments: 15 (1 by maintainers)

Most upvoted comments

Still not working ²

I also have a problem, which is probably related to a memory leak. When I’m rendering document to the file for the first time - everything is ok. Any second and next attempt (for example, after changing the list of maped children in Page component) is unsuccesful - there is an error. Looks like the memory is not erased after first rendering.

Child already has a parent, it must be removed first.

The above error occurred in the <VIEW> component: in VIEW (created by ProjectMap) in VIEW (created by ProjectMap) in VIEW (created by ProjectMap) in PAGE (created by ProjectMap) in ProjectMap in DOCUMENT

Uncaught (in promise) abort() at Error at jsStackTrace (ROOT\node_modules\yoga-layout-prebuilt\yoga-layout\build\Release\nbind.js:443:15) at stackTrace (ROOT\node_modules\yoga-layout-prebuilt\yoga-layout\build\Release\nbind.js:453:14) at Object.abort (ROOT\node_modules\yoga-layout-prebuilt\yoga-layout\build\Release\nbind.js:9808:182) at _abort (ROOT\node_modules\yoga-layout-prebuilt\yoga-layout\build\Release\nbind.js:585:20) at ge (ROOT\node_modules\yoga-layout-prebuilt\yoga-layout\build\Release\nbind.js:2905:136) at Vd (ROOT\node_modules\yoga-layout-prebuilt\yoga-layout\build\Release\nbind.js:2443:101) at ec (ROOT\node_modules\yoga-layout-prebuilt\yoga-layout\build\Release\nbind.js:1928:23) at kc (ROOT\node_modules\yoga-layout-prebuilt\yoga-layout\build\Release\nbind.js:1951:86) at Ig (ROOT\node_modules\yoga-layout-prebuilt\yoga-layout\build\Release\nbind.js:3444:37) at hm (ROOT\node_modules\yoga-layout-prebuilt\yoga-layout\build\Release\nbind.js:4785:228)

I’m using v1.6.7.

Still not working, the render time getting longer when i load the same documents multiple times in web, example render first time need 2s then 3-4s, 5-6s, 7-8s.

@will-evers So after implementing the proposed fix in our production application, we also still see leaks. So I believe you are correct, while that change certainly helps in this example, there is still a problem left unsolved in regards to the memory leak.