react-pdf icon indicating copy to clipboard operation
react-pdf copied to clipboard

Memory Leak using node

Open carlosmeds opened this issue 10 months ago • 7 comments

Describe the bug Unfortunatelly, using React-PDF/renderer is giving us a memory leak. We tried to remove all listeners, tried different methods, like renderToStream, renderToBuffer and updateContainer but it keeps increasing memory usage.

Desktop (please complete the following information):

  • OS: linux
  • React-pdf version 4.1.6

carlosmeds avatar Jan 30 '25 18:01 carlosmeds

I'll need a way to replicate this

diegomura avatar Feb 05 '25 22:02 diegomura

There seem to be still or new memory leak in loadYoga

Image

Retained size of instancePromise is getting out of hand.

We are using react-pdf in backend and rendering pdf's on the fly just simply callingawait renderToBuffer(<ThePdf {...props} />)

nnaku avatar Feb 11 '25 11:02 nnaku

I'll need a way to replicate this

Leak will show up at debugger with this.

import React from "react";
import { renderToBuffer } from "@react-pdf/renderer";
import { Quixote } from "./pdf.js";   // copy from https://react-pdf.org/repl

for (const i of Array(100).keys()) {
  await renderToBuffer(<Quixote />);
}

nnaku avatar Feb 12 '25 15:02 nnaku

downgrading yoga-layout to 3.0.4 removes the memory leak

❯ yarn why yoga-layout
└─ @react-pdf/layout@npm:4.2.2
   └─ yoga-layout@npm:3.2.1 (via npm:^3.2.1)
Image

and after setting resolution for yoga-layout: 3.0

❯ yarn why yoga-layout
└─ @react-pdf/layout@npm:4.2.2
   └─ yoga-layout@npm:3.0.4 (via npm:3.0)
Image

nnaku avatar Feb 12 '25 20:02 nnaku

is there are any progress? can we make using Yoga as WASM optional/configurable?

khakulov avatar Apr 07 '25 04:04 khakulov

I have fixed it with those dependencies and no longer problems with memory leak

"dependencies": {
    "@react-pdf/renderer": "3.4.5",
    "yoga-layout-prebuilt-low-memory": "^1.9.8"
},
"resolutions": {
    "yoga-layout-prebuilt": "yoga-layout-prebuilt-low-memory"
}

knapeto avatar Apr 07 '25 04:04 knapeto

Hi everyone, I found the problem with the yoga-layout and why downgrading to 3.0.4 works https://github.com/facebook/yoga/issues/1572#issuecomment-3212073396

yoga reports warnings when measure function does not return a positive number for either width or height. With the current yoga WASM setup, warnings are never output to the browser console. It seems the console log messages are buffered and kept in memory until the WASM lib is unloaded.

While it's definitely a problem on yoga side, once they fix the issue react-pdf would start producing a lot of console warnings as returning undefined width or height happens in a few measureFn implementations in this repo.

Even before yoga fixes the bug, react-pdf can be changed to mitigate the issue by always returning a positive number for width and height

Places that need to change:

  • Fix typings: make width and height required number https://github.com/diegomura/react-pdf/blob/ee5c96b80326ba4441b71be4c7a85ba9f61d4174/packages/layout/globals.d.ts#L134-L135
  • Explicitly set width and/or height to 0 where it's missing to avoid yoga-layout reporting and issue and falling back to 0 anyway https://github.com/diegomura/react-pdf/blob/ee5c96b80326ba4441b71be4c7a85ba9f61d4174/packages/layout/src/image/measureImage.ts#L38-L60 https://github.com/diegomura/react-pdf/blob/ee5c96b80326ba4441b71be4c7a85ba9f61d4174/packages/layout/src/text/measureText.ts#L29 https://github.com/diegomura/react-pdf/blob/ee5c96b80326ba4441b71be4c7a85ba9f61d4174/packages/layout/src/text/measureText.ts#L46 https://github.com/diegomura/react-pdf/blob/ee5c96b80326ba4441b71be4c7a85ba9f61d4174/packages/layout/src/svg/measureSvg.ts#L32 https://github.com/diegomura/react-pdf/blob/ee5c96b80326ba4441b71be4c7a85ba9f61d4174/packages/layout/src/svg/measureSvg.ts#L35

@diegomura does it sound reasonable? Would you accept a PR with the proposed changes?

Obi-Dann avatar Aug 24 '25 23:08 Obi-Dann