Skip to content

Memory leaks (?) when inserting images #714

@daniel-kukiela

Description

@daniel-kukiela

Please provide all mandatory information!

Describe the bug (mandatory)

When inserting images to the pdf files opened in a loop, memory usage constantly increases.

To Reproduce (mandatory)

Example code:

import fitz
import cv2
import numpy
import io
import numpy as np

# Infinitive loop
while True:
    # Empty document for this example
    with fitz.open() as doc:
        # Let's also instert 100 empty pages
        for i in range(100):
            doc.insertPage(i)
            page = doc[i]

            # Create image rectangle
            image_rectangle = fitz.Rect(0, 0, 10, 10)

            # Example image - 1000x1000 px zaved as png into byte stream
            image = np.zeros((100, 100, 3))
            _, image_numpy_bytes = cv2.imencode(".png", image, [cv2.IMWRITE_PNG_COMPRESSION, 9])
            image_bytes = io.BytesIO(image_numpy_bytes)

            # Inserting image - test with this commented out to see no memory leaks, then uncomment and check again
            #page.insertImage(image_rectangle, stream=image_bytes)

Expected behavior (optional)

Release all resources on doc.close()

Your configuration (mandatory)

  • Ubuntu 20.04
  • Python 3.8, 64bit
  • PyMuPDF 1.18.2 installed from pip

Additional context (optional)

We are working with a single and pretty big pdf file. We want to load it in a loop and replace some images (there are some other operations as well, but it looks like only image insertions cause memory leaks). This causes memory to quickly start filling-up. It looks like some resources are not being properly released when the document is closed.

Regards,
Daniel

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions