[Image-SIG] Serious PIL Memory Leak

Markus Gritsch gritsch@iue.tuwien.ac.at
Sun, 03 Mar 2002 20:31:48 +0100


This is a multi-part message in MIME format.
--------------060008050502040509050106
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

Hi!

I think I have discovered a serious memory leak in PIL when using the 
text method of the ImageDraw.Draw class.  The attached script reproduces 
the bug.  Try to remove the 100ms delay, and your memory gets filled 
really fast ;-)

Since I use this a lot in my webcam software 
(http://stud4.tuwien.ac.at/~e9326522/VideoCapture/), I would like to 
know if there is a workaround available?

Markus

--------------060008050502040509050106
Content-Type: text/plain;
 name="PIL_memory_leak.py"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="PIL_memory_leak.py"

from PIL import Image, ImageFont, ImageDraw
import time

im = Image.new('RGB', (320, 240), 0x888888)

font = ImageFont.load_path('helvB08.pil')
text = 'memory leak test'
textcolor = 0xffffff
shadowcolor = 0x000000
y = -1
x = 2

draw = ImageDraw.Draw(im)

i = 0
while (1):
    draw.text((x-1, y-1), text, font=font, fill=shadowcolor)
    draw.text((x+1, y-1), text, font=font, fill=shadowcolor)
    draw.text((x-1, y+1), text, font=font, fill=shadowcolor)
    draw.text((x+1, y+1), text, font=font, fill=shadowcolor)
    draw.text((x, y), text, font=font, fill=textcolor)

    #im.save('leak.bmp')

    # with this delay-value, approx. 1MB/s is leaked
    time.sleep(0.1)

    print i
    i += 1

--------------060008050502040509050106--