Memory leak when creating lots of object

Godzilla godzillaismad at gmail.com
Tue Aug 14 00:57:22 EDT 2007


Hello,

I have a program that create and pop an object off a queue, but it is
experiencing some memory leakage. I have been unable to detect where
the memory leakage occur. The strange thing is when i replace the
object creation with a plain integer/string, the leak goes away...
Here's the code I used as my test:


import Queue
import threading
import time

q = Queue.Queue(0)

class PDU:
    def __init__(self, name=''):
        self.name = name

class qTest(threading.Thread):
    def __init__(self, name=''):
        threading.Thread.__init__(self)
        self.name = name
        self.threadContinue = True

    def run(self):
        count = 0
        while self.threadContinue:
            pdu = q.get(True)
            del pdu
            count+=1
            #print count
            if q.qsize() == 0:
                print "Total get in bulk", count
                count = 0

    def stop(self):
        self.threadContinue = False
        q.put(1)


if __name__ == "__main__":
    try:
        qt = qTest()
        qt.start()
        loopCount = 1000
        while True:
            for i in range(loopCount):
                pdu = PDU()
##                pdu = 1
                q.put(pdu)
            time.sleep(5)

    except KeyboardInterrupt:
        print "Keyboard interrupted. Program exit."
    except Exception, why:
        print why

    if qt.isAlive():
        qt.stop()

I can see the memory usage increases slowly in Task Manager under XP,
but do not know why. Anyone help?

Thank you.




More information about the Python-list mailing list