Memory leak with images creation
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
}
I really need your help because I'm so bad at CGO.
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.
I've had the SAME problem with ImagePNG(0, 300.0)
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.
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!
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.