memory leak while convert Pixmap's colorspace · Issue #4125 · pymupdf/PyMuPDF (original) (raw)
Description of the bug
call Pixmap 's __init__(Colorspace, Pixmap) override will cause a memory leak when the source pixmap's colorspace don't equal fitz.csRGB and the target colorspace is fitz.csRGB.
How to reproduce the bug
pdf: demo.pdf
just run code to reproduce:
import fitz
import gc
import psutil
def main():
"""
Check for memory leaks.
"""
process = psutil.Process()
def get_rss():
return round(process.memory_info().rss / (1024 * 1024), 2)
stat = get_rss()
demo = "demo.pdf"
for i in range(10):
with fitz.open(demo) as doc:
for one_page in doc:
for info in one_page.get_images(full=True):
pix = fitz.Pixmap(doc, info[0])
if pix.colorspace != fitz.csRGB:
pix1 = fitz.Pixmap(fitz.csRGB, pix)
del pix1
del pix
break
fitz.TOOLS.store_shrink(100)
fitz.TOOLS.glyph_cache_empty()
gc.collect()
stat1 = get_rss()
print(
f"init RSS: {stat:.3f}MB -> current RSS: {stat1:.3f}MB, leak: {stat1-stat:.3f}MB"
)
stat1 = get_rss()
print(
f"init RSS: {stat:.3f}MB -> final RSS: {stat1:.3f}MB, leak: {stat1-stat:.3f}MB"
)
if __name__ == "__main__":
main()
PyMuPDF version
1.24.9
Operating system
Linux
Python version
3.12