473,651 Members | 3,004 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

embedding Python: how to avoid memory leaks?

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_SimpleStr ing("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
Mar 9 '06 #1
10 5656
Hallöchen!

Andrew Trevorrow <an****@trevorr ow.com> 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_SimpleStr ing("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
Mar 9 '06 #2
> > Our app (http://golly.sourceforge.net/) currently uses calls
like these every time a user decides to run a script:

Py_Initialize() ;
PyRun_SimpleStr ing("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
Mar 9 '06 #3
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_SimpleStr ing("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_SimpleStr ing("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_SimpleStr ing("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_SimpleStr ing shouldn't leak, and didn't when I
tried it.

Regards,
Martin
Mar 9 '06 #4
Hallöchen!

an****@trevorro w.com (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
Mar 9 '06 #5
Torsten Bronger <br*****@physik .rwth-aachen.de> wrote:
an****@trevorro w.com (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
Mar 10 '06 #6
ma****@v.loewis .de wrote:
I could reproduce a memory leak with the code

#include <Python.h>
int main()
{
while(1){
Py_Initialize() ;
PyRun_SimpleStr ing("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_SimpleStr ing("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_SimpleStr ing 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
Mar 10 '06 #7
Hallöchen!

an****@trevorro w.com (Andrew Trevorrow) writes:
Torsten Bronger <br*****@physik .rwth-aachen.de> wrote:
an****@trevorro w.com (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
Mar 10 '06 #8
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
Mar 10 '06 #9
Torsten Bronger wrote:
I couldn't get the PyRun_*File* calls to work on Windows,
presumabl y 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
Mar 10 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

68
5839
by: Lad | last post by:
Is anyone capable of providing Python advantages over PHP if there are any? Cheers, L.
34
6404
by: Ville Voipio | last post by:
I would need to make some high-reliability software running on Linux in an embedded system. Performance (or lack of it) is not an issue, reliability is. The piece of software is rather simple, probably a few hundred lines of code in Python. There is a need to interact with network using the socket module, and then probably a need to do something hardware- related which will get its own driver written in C.
0
1589
by: Robby Dermody | last post by:
Hey guys (thus begins a book of a post :), I'm in the process of writing a commercial VoIP call monitoring and recording application suite in python and pyrex. Basically, this software sits in a VoIP callcenter-type environment (complete with agent phones and VoIP servers), sniffs voice data off of the network, and allows users to listen into calls. It can record calls as well. The project is about a year and 3 months in the making and...
25
5734
by: abhinav | last post by:
Hello guys, I am a novice in python.I have to implement a full fledged mail server ..But i am not able to choose the language.Should i go for C(socket API) or python for this project? What are the advantages of one over the other in implementing this server.which language will be easier? What are the performance issues?In what language are mail servers generally written?
0
324
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 421 open ( -2) / 3549 closed (+10) / 3970 total ( +8) Bugs : 943 open (-17) / 6471 closed (+25) / 7414 total ( +8) RFE : 260 open ( +2) / 250 closed ( +1) / 510 total ( +3) New / Reopened Patches ______________________
3
3436
by: test.07 | last post by:
I am wondering what happens to a thread in python in relation to win32com extensions. If I create a new thread, that uses the Dispatch method from win32com, what happens to the memory allocated in that thread when the thread is done. Will the Dispatch release the memory it created, or will the memory remain? The problem rises from the fact that Dispatch does not seem to release memory correctly every time. If I include the commands in...
0
8700
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8465
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8581
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6158
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5612
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4144
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4285
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2701
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1910
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.