嵌入Python:如何避免内存泄漏? [英] embedding Python: how to avoid memory leaks?

查看:125
本文介绍了嵌入Python:如何避免内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对我的上一条消息没有回应,所以我会尝试不同的方法...


有没有人知道,或者甚至更好,这里有人写过

Mac / Windows的C ++应用程序,允许用户从应用程序中运行Python

脚本?不仅一次,而且在单个会话中多次使用
,并且没有泄漏内存。最好是一个

开源应用程序,这样我就可以看到它是如何完成的。


我们的应用程序( http://golly.sourceforge.net/ )每次用户决定使用时都会使用这样的电话

运行脚本:


Py_Initialize();

PyRun_SimpleString(" execfile(''foo.py'')");

Py_Finalize();


但即使foo.py为* empty *上述调用也会产生内存

泄漏大约12K on Windows 2000上的Mac OS 10.3.9(使用Python 2.3)和大约

11K(使用Python 2.4.2)。我不介意,如果由于第一次调用Py_Initialize非常元/ b $ b而导致一次性费用,但我们每次执行脚本时都会看到泄漏。


我曾尝试过一次调用Py_Initialize(在应用启动时)

和Py_Finalize一次退出,但这并没有真正解决/>
什么的。它避免了使用琐碎脚本时的泄漏(比如

一个空的.py文件!)但我们想运行一些相当复杂的
脚本,这些脚本占用大量内存,所以我们需要一种可靠的方式

来释放那个记忆。我很惊讶地发现

Py_Finalize并不总是这样做。


是否有一些可以恢复内存使用的神奇Python代码

到每个execfile调用之前是什么?类似

PostScript的保存和恢复。


我一直在努力解决这个问题大约一个星期。

被Python的力量和美丽诱惑我讨厌

不得不放弃它并切换到Perl或其他一些糟糕的

脚本语言!请帮忙...


Andrew

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

推荐答案

Hall?chen!

Andrew Trevorrow< an **** @ trevorrow.com>写道:
Hall?chen!

Andrew Trevorrow <an****@trevorrow.com> writes:
[...]

[...]不仅一次,而且在一次会议中多次,并且没有泄露记忆。最好是一个开源应用程序,这样我就能看到它是如何完成的。

我们的应用程序( http://golly.sourceforge.net/ )每次用户决定运行脚本时,都会使用类似这样的调用:

Py_Initialize();
PyRun_SimpleString(" execfile(''foo.py'')");
[...]

[...] 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'')");




PyRun_AnyFile是否显示相同的效果?这就是我要去的方式




Tsch?,

Torsten。


-

Torsten Bronger,aquisgrana,europa vetus ICQ 264-296-646



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


> >我们的应用程序( http://golly.sourceforge.net/ )目前使用了来电
> > Our app (http://golly.sourceforge.net/) currently uses calls
每次用户决定运行脚本时都这样:

Py_Initialize();
PyRun_SimpleString(" execfile('' foo.py'')");
like these every time a user decides to run a script:

Py_Initialize();
PyRun_SimpleString("execfile(''foo.py'')");



PyRun_AnyFile是否显示相同的效果?这就是我要去的方式。



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




我无法让PyRun_ * File *调用在Windows上运行,大概是

,因为文档中提到了FILE *问题。


如果它对内存产生任何影响我会非常惊讶/>
泄漏问题。让我知道你是如何上场的!


Andrew



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


Andrew Trevorrow写道:
Andrew Trevorrow wrote:
我们的应用( http://golly.sourceforge.net/ )目前正在使用电话
每次用户决定运行脚本时都喜欢这些:

Py_Initialize();
PyRun_SimpleString(" execfile(''foo.py'')");
Py_Finalize();

但即使foo.py为* empty *,上述调用也会导致Mac OS 10.3.9(使用Python 2.3)和大约12K的内存泄漏
11K在Windows 2000上(使用Python 2.4.2)。
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).




我可以使用代码重现内存泄漏


#include< Python.h>

int main()

{

while(1){

Py_Initialize();

PyRun_SimpleString(" execfile(''foo.py'')");

Py_Finalize();

}

}

但是,我无法用代码重现内存泄漏


#include< Python.h>

int main( )

{

Py_Initialize();

while(1){

PyRun_SimpleString(" execfile(' 'foo.py'')");

}

Py_Finalize();

}


所以我建议你只进行一次Py_Initialize。众所周知

初始化Python解释器会分配内存,而b / b
永远不会被释放,例如:扩展模块中的全局变量

(没有任何API可以告诉所有模块释放他们的

内存)。所以一个Py_Initialize / Py_Finalize的循环肯定会泄漏
.


OTOH,PyRun_SimpleString不应该泄漏,并且我不是什么时候>
试了一下。


问候,

马丁



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


这篇关于嵌入Python:如何避免内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆