Searching for solution: Severe memory leak, HELP!

Steve Holden sholden at
Sat Jan 18 16:48:01 CET 2003

"Randy Young" <randall_young at> wrote in message
news:8338c7cc.0301180618.5c67da85 at
> Sirs,
> I have a very large Python application squarely in my lap, that's
> leaking almost 20mb of main memory an hour!
> I spent most of last night looking through various postings, and not
> surprisingly, I'm wondering if I have a "circular reference" at work.
> Specifically, something happening with the heavy use of the "exec"
> library call.
> Here's a code snippet, can anyone see anything grievously wrong? I
> understand that we may need to look at WHAT is being "exec'd", but I
> thought this might be a place to start...
> #import the module, then execute the probe
>   where = 'import'
>   win32event.WaitForSingleObject(self.iSemaphore, win32event.INFINITE)
>     try:
>       exec ("import %s" % probeInfo['ImportName'])
>     except Exception, e:
>       ret = "Import error on " + str(probeInfo['ImportName']) + " " +
> str(e)
>       self.ExceptOut(e, 'Exception importing Probe ID ' +
> str(probeInfo['ID']))
>     win32event.ReleaseSemaphore(self.iSemaphore, 1)
>     if (ret == None):
>       where = 'exec'
>       try:
> s = "ret = %s.%s (probeInfo)" % (probeInfo['ImportName'],
> probeInfo['Invoke'])
>         exec (s)
> # ... much more yada-yada ...
> ---- CODE ENDS
> As you can see there are "exec(s)" all over the place in the module,
> one mistake would be magnified many times! I'm trying to understand if
> I should be forcing a garbage collection or some other obvious thing?
> Thanks in advance for the consideration of a reply.

Well, firstly it might be better to use



    exec "import ..."

Also note, by the way, that exec is a statement and not a function, so the
parentheses are redundant.

What are you trying to import? It's conceivable that failed imports of a
badly-constructed extension might leak memory. Also note that if you leave
pointers to the stack frame lying about in "except" clauses that can cause

Just a few random thoughts.

Steve Holden                        
Python Web Programming       
Bring your musical instrument to PyCon!

More information about the Python-list mailing list