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

Memory issue with creating a doc with a lot of images #861

Closed
rsshilli opened this issue Aug 24, 2018 · 2 comments · Fixed by #889
Closed

Memory issue with creating a doc with a lot of images #861

rsshilli opened this issue Aug 24, 2018 · 2 comments · Fixed by #889

Comments

@rsshilli
Copy link

I'm creating a document that has 750+ pages where each page has a different screenshot. The total doc size is 48 MB and it takes ~4 mins to create, which seems fine. What's not fine is that it take ~6 GB of memory to create this 48 MB file.

At first I thought I had the same problem as #728 but when I tried @blikblum's branch which includes both the new es6 conversion and #728 I found that it didn't help at all.

I've figured out a fix for it. The problem is in the PNG zlib decrypting. The doc.image() method is returning right away, and if you create a lot of pages and add a lot of images, the image processing piles up on itself until it's literally trying to decode hundreds of images at once. Unfortunately it seems the image decoding takes a few x more memory and when it happens all at once it gets out of hand.

With my fix, the 6 GB of memory went down to about 100 MB of memory. The downside though is that now doc.image() returns a promise instead of the doc, which means they can't be chained together anymore.

@rsshilli
Copy link
Author

For those looking for a work-around for version 0.8.x, try this:

    async function writeImage(someImage) {
      doc.image(someImage);
      // Sleep for a bit because of https://github.com/foliojs/pdfkit/issues/861
      await new Promise(resolve => setTimeout(() => resolve(), 100));
    }

@blikblum
Copy link
Member

Probably fixed by #889 . The issue still can occur because png-js decodePixels calls zlib inflate async, but odds are much smaller.

Please test and if still occurs reopen with a sample code

@blikblum blikblum mentioned this issue Jan 17, 2025
4 tasks
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

Successfully merging a pull request may close this issue.

2 participants