This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author kauboy
Recipients Kaushik.Kannan, barry, kauboy, r.david.murray
Date 2011-03-28.08:54:04
SpamBayes Score 7.528496e-08
Marked as misclassified No
Message-id <1301302445.42.0.429068161494.issue11693@psf.upfronthosting.co.za>
In-reply-to
Content
I wrote a daemon to monitor a directory and send out emails periodically. To create the email, I used MIMEMultipart() object. When as_string() method is called on the MIMEMultipart() object, it seemed to cause memory leaks. On looking at the as_string() method, I saw that the  email.generator.Generator().flatten() method call is causing the memory leak. I copied the as_string() method out as a function and used it for tracing the memory leak.


#!/usr/bin/python

from guppy import hpy
import time
import gc

from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from cStringIO import StringIO
from email.generator import Generator

def as_string_mod(msg, unixfrom=False):
        """Return the entire formatted message as a string.
        Optional `unixfrom' when True, means include the Unix From_ envelope
        header.

        This is a convenience method and may not generate the message exactly
        as you intend because by default it mangles lines that begin with
        "From ".  For more flexibility, use the flatten() method of a
        Generator instance.
        """
        fp = StringIO()
        g = Generator(fp)
        g.flatten(msg, unixfrom=unixfrom)
        return fp.getvalue()

msg = MIMEMultipart()
msg['From'] = 'from@gmail.com'
msg['To'] = 'to@gmail.com'
msg['Subject'] = 'Function'
msg.attach(MIMEText('Blah'))

if __name__=='__main__':
	while True:
		as_string_mod(msg)
		gc.collect()
		print hpy().heap()
		time.sleep(5)
History
Date User Action Args
2011-03-28 08:54:05kauboysetrecipients: + kauboy, barry, r.david.murray, Kaushik.Kannan
2011-03-28 08:54:05kauboysetmessageid: <1301302445.42.0.429068161494.issue11693@psf.upfronthosting.co.za>
2011-03-28 08:54:04kauboylinkissue11693 messages
2011-03-28 08:54:04kauboycreate