Memory Leak using node
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
I'll need a way to replicate this
There seem to be still or new memory leak in loadYoga
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} />)
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 />);
}
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)
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)
is there are any progress? can we make using Yoga as WASM optional/configurable?
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"
}
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
widthandheightrequirednumberhttps://github.com/diegomura/react-pdf/blob/ee5c96b80326ba4441b71be4c7a85ba9f61d4174/packages/layout/globals.d.ts#L134-L135 - Explicitly set
widthand/orheightto0where 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?