5

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?

Jongware
  • 22,200
  • 8
  • 54
  • 100
Pawel Miech
  • 7,742
  • 4
  • 36
  • 57
  • 3
    have you tried [Image.close()](http://pillow.readthedocs.org/en/latest/reference/Image.html#PIL.Image.Image.close) ? – Gil May 23 '15 at 13:13
  • @Gil no, I need to check that, will try and report what happens. – Pawel Miech May 23 '15 at 13:18
  • Not sure (so a comment): you are converting to RGBA. Image #1 is RGB and since `convert` returns *a copy*, you are overwriting your `img` variable (source: PIL, `Image.py` ln 633: "Returns a converted copy of this image"). I am not sure this is the problem because it should also return a copy if the image already is in the correct space (your img #2). – Jongware May 23 '15 at 13:42
  • image.close() does not release memory as well. I get same output @Gil – Pawel Miech May 23 '15 at 13:43
  • @PawelMiech Apologies, I'm stumped then! – Gil May 23 '15 at 13:48
  • Could be an os issue. What happens to the memory usage if you repeat the image manipulation several times in the same function? – het.oosten May 23 '15 at 14:04
  • @het.oosten I use ubuntu 14.04 I asked friend on mac and he was able to reproduce, he also got higher memory usage – Pawel Miech May 23 '15 at 14:38
  • @PawelMiech did you ever find a solution to this? I just came across a VERY similar issue yesterday. I have an application that is bulk processing images with Pillow. There's no problem until I start saving the images to my filesystem. Huge memory leak and I can't figure it out. Tested on both Mac and Windows machines. – Gil May 27 '15 at 16:09
  • 1
    @Gil no i havent found solution. I'm going to open ticket in pil repo will post a link here maybe one of PIL developers will help us – Pawel Miech May 28 '15 at 17:30
  • @PawelMiech did you by any chance find a solution to this? – Timothy Dalton Jan 01 '21 at 19:40
  • @TimothyDalton nope, I found some workaround for my specific case, but it was very application specific, not applicable outside my use case. – Pawel Miech Jan 02 '21 at 21:25

0 Answers0