Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > embedding Python: how to avoid memory leaks?

Reply
Thread Tools

embedding Python: how to avoid memory leaks?

 
 
Andrew Trevorrow
Guest
Posts: n/a
 
      03-09-2006
No response to my last message, so I'll try a different tack...

Does anyone know of, or even better, has anyone here written a
C++ application for Mac/Windows that allows users to run Python
scripts from within the app? Not just once, but many times in
a single session, and without leaking memory. Preferably an
open source app so I can see how it's done.

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 wouldn't mind if
there was a one-off cost due to calling Py_Initialize the very
first time, but we see leaks every time a script is executed.

I've tried calling Py_Initialize just once (at app startup)
and Py_Finalize once on exit, but that doesn't really solve
anything. It avoids leaks when using trivial scripts (like
an empty .py file!) but we want to run some rather complicated
scripts that consume lots of memory, so we need a reliable way
to release that memory. I was surprised to discover that
Py_Finalize doesn't always do that.

Is there some magic Python code that can restore memory usage
to what it was before each execfile call? Something like
PostScript's save and restore.

I've been struggling with this problem for about a week now.
Having been seduced by Python's power and beauty I'd hate to
have to abandon it and switch to Perl or some other crappy
scripting language! Please help...

Andrew
 
Reply With Quote
 
 
 
 
Torsten Bronger
Guest
Posts: n/a
 
      03-09-2006
Hallöchen!

Andrew Trevorrow <> writes:

> [...]
>
> [...] Not just once, but many times in a single session, and
> without leaking memory. Preferably an open source app so I can
> see how it's done.
>
> 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')");


Does PyRun_AnyFile show the same effect? That's the way I'm about
to go.

Tschö,
Torsten.

--
Torsten Bronger, aquisgrana, europa vetus ICQ 264-296-646
 
Reply With Quote
 
 
 
 
Andrew Trevorrow
Guest
Posts: n/a
 
      03-09-2006
> > 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')");

>
> Does PyRun_AnyFile show the same effect? That's the way I'm about
> to go.


I couldn't get the PyRun_*File* calls to work on Windows, presumably
because of the FILE* problem mentioned in the docs.

I'll be very surprised if it makes any difference to the memory
leak problem. Let me know how you get on!

Andrew
 
Reply With Quote
 
=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
Guest
Posts: n/a
 
      03-09-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
 
Reply With Quote
 
Torsten Bronger
Guest
Posts: n/a
 
      03-09-2006
Hallöchen!

(Andrew Trevorrow) writes:

> [...]
>
> I couldn't get the PyRun_*File* calls to work on Windows, presumably
> because of the FILE* problem mentioned in the docs.


Which compiler do you use?

Tschö,
Torsten.

--
Torsten Bronger, aquisgrana, europa vetus ICQ 264-296-646
 
Reply With Quote
 
Andrew Trevorrow
Guest
Posts: n/a
 
      03-10-2006
Torsten Bronger <> wrote:

> (Andrew Trevorrow) writes:
>
> > [...]
> >
> > I couldn't get the PyRun_*File* calls to work on Windows, presumably
> > because of the FILE* problem mentioned in the docs.

>
> Which compiler do you use?


MSVC++ (version 6 from memory -- I do most of my development on the
Mac and fire up Virtual PC occasionally to test Win builds).

Andrew
 
Reply With Quote
 
Torsten Bronger
Guest
Posts: n/a
 
      03-10-2006
Hallöchen!

(Andrew Trevorrow) writes:

> Torsten Bronger <> wrote:
>
>> (Andrew Trevorrow) writes:
>>
>>> [...]
>>>
>>> I couldn't get the PyRun_*File* calls to work on Windows,
>>> presumably because of the FILE* problem mentioned in the docs.

>>
>> Which compiler do you use?

>
> MSVC++ (version 6 from memory -- I do most of my development on
> the Mac and fire up Virtual PC occasionally to test Win builds).


Well, I don't really *know*, but it's hard to believe to me that the
file descriptor format changed within the Microsoft product series.

Tschö,
Torsten.

--
Torsten Bronger, aquisgrana, europa vetus ICQ 264-296-646
 
Reply With Quote
 
Andrew Trevorrow
Guest
Posts: n/a
 
      03-10-2006
wrote:

> 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.


Surely that's a bug that should be fixed. There should be some way
to tell Python "release all the memory you've ever allocated and
start again with a clean slate".

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


Ok, after reading http://evanjones.ca/python-memory.html I think I
understand what's happening. Apparently the Python memory allocator
never releases memory back to the OS! So if a complicated script
happens to consume 100MB of interpreter memory then that amount is
no longer available to the app in which Python is embededded.
Even worse, if a script has a (Python) memory leak then there's
nothing the app can do about it. It would be great if wrapping
each script inside Py_Initialize/Py_Finalize could avoid all that.

I've been told that the next version of Python will release memory,
so that's good news. You can get it now if you're willing to build
Python from the latest source code.

Andrew
 
Reply With Quote
 
=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
Guest
Posts: n/a
 
      03-10-2006
Andrew Trevorrow wrote:
> Surely that's a bug that should be fixed. There should be some way
> to tell Python "release all the memory you've ever allocated and
> start again with a clean slate".


This bug cannot be fixed in any foreseeable future.

> I've been told that the next version of Python will release memory,
> so that's good news. You can get it now if you're willing to build
> Python from the latest source code.


That still won't release all memory - only the arenas that don't
have live Python objects on them anymore.

Regards,
Martin
 
Reply With Quote
 
=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
Guest
Posts: n/a
 
      03-10-2006
Torsten Bronger wrote:
>>>>I couldn't get the PyRun_*File* calls to work on Windows,
>>>>presumably because of the FILE* problem mentioned in the docs.


> Well, I don't really *know*, but it's hard to believe to me that the
> file descriptor format changed within the Microsoft product series.


The layout of the FILE type indeed didn't change. However, passing
FILE* across different versions of msvcrt will still crash; google
for details. In particular, if you build an application with VC6,
it will be linked with msvcrt4.dll. If you combine this with Python
2.4 (which is linked with msvcr71.dll), you cannot use PyRun_*File*.

Regards,
Martin
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Avoid memory corruption in shared memory used by several processes? Sune C Programming 14 08-26-2007 03:57 AM
Avoid memory corruption in shared memory used by several processes? Sune C Programming 5 07-13-2007 02:29 PM
Avoid having a SQL express for web parts and avoid personalization Roger23 ASP .Net 2 10-12-2006 10:54 PM
Avoid wasting time or how to avoid initialization Alexander Malkis C++ 8 04-13-2004 11:23 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57