-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Memory Leak #378
Comments
Hey @coreyweidenhammer I don't know wat may be causing this to be honest. My first guess is that react-pdf caches assets in memory to avoid network request for the same item, but it has a limit, so the memory footprint should stop increasing eventually. I'll try to find some time to look a it, but it's going to be hard to debug. |
Related to foliojs/pdfkit#258 ? |
i also experience memory leaks on client rendering but only on some devices |
It just happens on some client machines of different kind, Android, Mac etc. but I can't reproduce it myself. |
tl;dr: Each render leaks PDFKit objects. I've been running into this issue as well and put together a small reproduction gist: https://gist.github.com/n6g7/4d9d9af8eb5b943b61c89c1c6fa29a3b When running this on my mac I get the data in this spreadsheet. I also used node-heapdump to inspect the objects in memory and I noticed many I don't really understand what's happenign with pdfkit, its last release was 2 years ago (v0.8.3) but there's been a lot of activity since then on the repo, which doesn't appear to have been released. I assume this is why react-pdf uses a fork ( Next step is to reproduce the leak using pdfkit only, could someone point to the source code of @react-pdf/pdfkit? |
Thanks for your input and work on this! |
@kations the problem is just |
@Skywalker13 I get similar results with a patched version of |
I suppose that you use react-pdf in frontend (browser side) and not in server side (with babel-node like me). In this case you can continue to use the asm.js version. Look at the If I build for node by hand:
I have a node module for nbind which is used by
No more And if you want to build for the browser, you must install an emscripten environment and change the diff --git a/javascript/final-flags.gypi b/javascript/final-flags.gypi
index dd13e827..d3e1ad26 100644
--- a/javascript/final-flags.gypi
+++ b/javascript/final-flags.gypi
@@ -19,7 +19,7 @@
"ldflags": [
"--memory-init-file", "0",
"-s", "PRECISE_F32=1",
- "-s", "TOTAL_MEMORY=134217728"
+ "-s", "TOTAL_MEMORY=268435456"
],
"xcode_settings": { And try to run |
We’ve encountered these heap overflow errors as well. They happen reliably when we try to generate documents with a large number of pages (~70 pages), or when we render a large number of separate documents with a single page. |
@diegomura It looks like |
Have encountered this issue as well. Worked around it by moving the PDF generation to an AWS lambda for now. @diegomura do you need help pulling the changes in foliojs/pdfkit#258 (comment) to your own |
Yes!! Help is much appreciated. Don't have much time to that yet @Tketa |
@Tketa let me know if I can help! |
@Tketa @diegomura @n6g7 I think this is critical to anyone using
How does that sound to you ? |
@maxaggedon Sounds good to me, I'm quite busy this week so can't commit to opening the PR and resolving conflicts but will review and test in my projects for sure. |
Also sounds good to me. I'm also in the middle of something right now (react-pdf related also), so I'm not sure if I can commit to work on this right now. I agree with your plan and I promise I'll review code when needed |
Hey @maxaggedon, agree with the plan but as mentioned I fixed the issue with the serverless solution so not a priority for me this week, can work on it this weekend however 👍 |
know everyone is on board ! I will try to start the work during the week. |
PR is open 👆! |
Thanks @maxaggedon !! You rock |
I've tried to pinpoint where exactly is @n6g7 gist leaking and made a couple of changes to
[edit] |
@diegomura This is in both Somewhere in the code must be a call to I think you were actually on the right path in this comment Just to confirm that this is an issue I tried
For a page with The question is where should measureFunc be unset? |
Amazing progress, thanks @bhunjadi!!
This looks like a very promising lead now. :) @diegomura any ideas where would be the correct place to call |
Thanks for this amazing research! Sorry I've been a bit unresponsive. I guess a good place to call One of the main performance bottlenecks of this project is the page wrapping algorithm. As I said, in order to contemplate all cases, it clones and re-layout many times, which makes it very hard to debug and uses a lot of memory. I've been trying to think of a simpler solution but couldn't find one yet that supports all what we currently do. And there's no prior state of the art solution to this (that I could find). |
I've added PR that looks like it is working. Thanks @n6g7 for the |
Thank you to everyone who has worked on this! My team is still wrapping up testing all of our scenarios, but preliminary results from v1.5.6 appear to confirm that the memory leak is indeed resolved. I'll post another update when we are finished. |
Thank you @coreyweidenhammer for testing! Please post any further details. Closing this issue 😄 |
EDIT: Seems I'm having another problem, will open a new issue 😄 |
Anyone got around of building for Yoga for |
Patches exists for nbind and node v12, but it's annoying because it's necessary to fork yoga-layout in order to use an other nbind. And even in this case it's annoying because it breaks with electron 8 and in my case, I need yoga with node 12 and electron 8. I've tried yoga-wasm but it's broken with react-pdf: rickbutton/yoga-wasm#4 And just now, I've discovered https://github.com/pinqy520/yoga-layout-wasm I will try this one asap... |
@Skywalker13 any luck on your try outs? We have no solution for this so far other than manually restarting the process :( |
I tried a bit, it's not just a drop-in replacement. It's necessary to fork and adapt a little bit react-pdf (because the init is async with the wasm variants like yoga-layout-wasm), and maybe it doesn't work (like yoga-wasm), I don't know. I've some success with react-pdf and yoga-layout built for some node versions but it's a bit annoying (for example Xcraft-Inc/yoga@a0039ce). IMHO It's definitively react-pdf which should use something else... |
Before I get into the issue, I just wanted to thank @diegomura and others for your work and responsiveness on this wonderful library! Your efforts are greatly appreciated.
OS:
React-pdf version:
1.0.0-alpha.24, 1.0.0-alpha.25
Node v8.9.3
Description:
We are consistently observing a memory leak, with the memory footprint of our node process growing linearly with each generated PDF until eventually exhausting the resources on our servers.
My team saw #165 (now closed), but we are uncertain as to whether what we are experiencing is related or not. As noted above, we have tried using both alpha.24 and alpha.25 with the same results.
We first noticed the memory leak on servers running in AWS via ElasticBeanstalk (Amazon Linux) but have since been able to recreate it on our local development environments running macOS 10.13.6. We attempted isolating the source of the leak ourselves but have not yet come up with anything conclusive. We're not even sure if the memory leak is (as before) related to a dependency vs the react-pdf code itself, but we are hoping you might be able to identify something more readily.
How to replicate issue:
We created a small project with limited dependencies that should allow you to recreate the issue as we see it. Zip attached here:
pdfGenerator.zip
There are instructions in server.js, repeated below:
RUN $ yarn / npm install and then run: $ npm run start-with-chrome-inspect
In the devtools window, select the Memory tab at the top.
Select 'Heap snapshot'.
Click 'Take snapshot'.
An alternate path to viewing the memory leak at a higher level is to monitor via pm2:
$ yarn / npm install pm2
$ pm2 start server.js
$ pm2 monit
-- proceed to access http://localhost:3000/pdf repeatedly
-- and watch the memory footprint grow.
Note that the memory footprint grows in proportion to the size of the PDF generated, so if you want to see it grow/leak more readily, you can create a larger PDF with more content, images, etc.
Thank you in advance for your help!
The text was updated successfully, but these errors were encountered: