embedding Python: how to avoid memory leaks?

"Martin v. Löwis" martin at v.loewis.de
Thu Mar 9 08:04:17 EST 2006


Andrew Trevorrow wrote:
> Our app (http://golly.sourceforge.net/) currently uses calls
> like these every time a user decides to run a script:
> 
>    Py_Initialize();
>    PyRun_SimpleString("execfile('foo.py')");
>    Py_Finalize();
> 
> But even if foo.py is *empty* the above calls result in a memory
> leak of about 12K on Mac OS 10.3.9 (using Python 2.3) and about
> 11K on Windows 2000 (using Python 2.4.2).

I could reproduce a memory leak with the code

#include <Python.h>
int main()
{
  while(1){
    Py_Initialize();
    PyRun_SimpleString("execfile('foo.py')");
    Py_Finalize();
  }
}

However, I could not reproduce a memory leak with the code

#include <Python.h>
int main()
{
  Py_Initialize();
  while(1){
    PyRun_SimpleString("execfile('foo.py')");
  }
  Py_Finalize();
}

So I recommend you do Py_Initialize only once. It is well-known
that initializing the Python interpreter allocates memory that
can never be freed, e.g. global variables in extension modules
(there just isn't any API to tell all the modules to release their
memory). So a cycle of Py_Initialize/Py_Finalize will certainly
leak.

OTOH, PyRun_SimpleString shouldn't leak, and didn't when I
tried it.

Regards,
Martin



More information about the Python-list mailing list