Let's say I have a service that gets images from the web and saves them do directory. I am getting input as .png files and saving them locally. I use PIL to open images, and add background to them if needed. I recently noticed high memory usage that takes place when processing some .png files. It seems that when opening some .png, PIL is not releasing memory properly.
Following code will demonstrate what happens:
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 for images like this one) this generates following output:
12416
12416
22508
22508
22508
22508
You can see that memory used nearly doubled! And even at the end when I deleted all objects memory is still twice what it was at the beginning.
For other images, for example this one memory usage doesn't change:
12416
12416
12416
12416
12416
12416
Does anyone knows why this happens? Is there some way I could avoid this?