go-fitz icon indicating copy to clipboard operation
go-fitz copied to clipboard

Memory leak with images creation

Open Athin0 opened this issue 7 months ago • 5 comments

When I load tested this code, memory usage kept growing and wasn't freed.

func MakeImageFirstPage(file []byte, DPI float64) (image.Image, error) {
	doc, err := fitz.NewFromMemory(file)
	if err != nil {
		return nil, fmt.Errorf("%w: %w", ErrInvalidFile, err)
	}
	defer doc.Close()
	img, err := doc.ImageDPI(0, DPI)
	if err != nil {
		return nil, fmt.Errorf("%w: %w", ErrImageCreation, err)
	}
	return img, nil
}

Image

I really need your help because I'm so bad at CGO.

Athin0 avatar May 06 '25 11:05 Athin0

New additional information:

  • When I run only fitz.NewFromMemory(...), memory remains stable.
  • When I increase the DPI (e.g. from 50 to 200), the memory usage grows faster and does not clear.

It looks like something inside ImageDPI or the underlying C resources might not be fully released.

Maybe I need to do something manually to free the memory after calling ImageDPI?

Thanks in advance for any help or suggestions.

Athin0 avatar May 07 '25 08:05 Athin0

I've had the SAME problem with ImagePNG(0, 300.0)

pmilanez avatar Jul 28 '25 15:07 pmilanez

hi @Athin0, were you able to solve the issue? I notice there is a similar issue in PyMuPDF and they provide a solution to set pix as None. https://github.com/pymupdf/PyMuPDF/issues/1430.

kj686 avatar Sep 09 '25 22:09 kj686

Hi @Athin0,

We also use go-fitz for image creation, and I suspect this might be the reason for the OOMs in our pods. While investigating, I came across this thread. Could you provide more details about:

The version of Go you were using

The versions of your dependencies

Details about your environment (OS, memory, etc.)

I’m unable to reproduce this memory leak on my local setup.

Thanks!

utsav82 avatar Oct 26 '25 15:10 utsav82

It would be nice if we had a reproducible example; probably a web server would be best for that. There were issues before, but most have already been solved. How I am using the library, I cannot reproduce nor have issues.

gen2brain avatar Oct 26 '25 15:10 gen2brain