Why doesn't PIL free memory after opening some png files?
Let's say I have a service that gets images from the internet and stores them in a directory. I receive the input as .png files and save it locally. I am using PIL to open images and add a background to them as needed. I recently noticed high memory that occurs when processing some .png files. It seems that when opening some .png, the PIL does not free the memory properly.
The following code will demonstrate what is happening:
from PIL import Image
import resource
def check_mem(filename):
mem = lambda:resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
print mem()
img = Image.open(filename)
print mem()
img = img.convert('RGBA')
print mem()
background = Image.new("RGBA", img.size, (255, 255, 255))
print mem()
image = background.paste(img, img)
print mem()
del background
del img
del image
print mem()
if __name__ == "__main__":
import sys
check_mem(sys.argv[1])
For some images (for example, images like this one ) this generates the following output:
12416 12416 22508 22508 22508 22508
You can see that the memory is almost doubled! And even at the end, when I deleted all objects, the memory is still twice as large as it was at the beginning.
For other images for an example of this , the memory usage does not change:
12416 12416 12416 12416 12416 12416
Does anyone know why this is happening? Is there a way to avoid this?
source to share
No one has answered this question yet
See similar questions:
or similar: