嵌入式Python 3解释器是否会泄漏内存? [英] Does the Python 3 interpreter leak memory when embedded?

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

问题描述

此错误报告指出,自2007年6月起,Python解释器将无法清理所有已分配的内容在带有嵌入式Python解释器的C/C ++应用程序中调用Py_Finalize之后的内存.建议在应用程序终止时调用一次Py_Finalize.

This bug report states that the Python interpreter, as of June 2007, will not clean up all allocated memory after calling Py_Finalize in a C/C++ application with an embedded Python interpreter. It was recommended to call Py_Finalize once at application termination.

此错误报告指出,从3.3版到2011年3月,解释器仍在泄漏内存.

This bug report states that as of version 3.3 and March 2011 the interpreter still leaks memory.

有人知道此问题的当前状态吗?我很担心,因为我有一个应用程序,其中每个运行中的实例都多次调用解释器,并且遇到内存泄漏.

Does anyone know the current state of this issue? I am concerned because I have an application in which the interpreter is called multiple times per running instance and I am experiencing memory leaks.

我已经在使用boost :: python来处理引用计数,并且清除了在两次运行之间运行Python程序所创建的所有引用的全局字典.我有一些单例课程-可能是问题所在吗?

I am already using boost::python to handle reference counts and I clear the global dictionary of all references created by running a Python program in between runs. I have some singleton classes - might these be the problem?

这是一个棘手的问题,还是Python解释器中的错误?

Is this a tractable issue or is it a bug in the Python interpreter?

推荐答案

您可以看到该错误(第一个错误,始于2007年)被nnorwitz关闭为"wontfix",并且其帖子在错误报告中./p>

You can see that the bug (the first one, from 2007) is closed as "wontfix" by nnorwitz, and his post is in the bug report.

为什么您多次呼叫Py_Initialize/Py_Finalize? 为什么不做这样的事情(我有点混用C和 为了方便起见,使用Python):

Why do you call Py_Initialize/Py_Finalize more than once? Why not do something like this (I'm kinda mixing C and Python for convenience):

/* startup */
Py_Initialize();

/* do whatever */
while (moreFiles()) {
    PyRun_SimpleString("execfile('%s')" % nextFile());
    /* do whatever */
}

/* shutdown */
Py_Finalize();

问题在于,大多数编写Python模块的人都不必担心如果模块被完成并重新初始化会发生什么,并且通常不关心在完成过程中进行清理.模块作者知道,当进程退出时,所有的内存都被释放了,别无所求.

The problem is that most people who write Python modules don't worry about what happens if their module gets finalized and reinitialized, and often don't care about cleaning up during finalization. Module authors know that all memory is released when the process exits, and don't bother with anything more than that.

因此,这实际上不是一个错误,而是一千个错误-每个扩展模块一个.影响一个影响少数用户的错误的工作量很大,其中大多数用户都有可行的解决方法.

So it's not really one bug, it's really a thousand bugs -- one for each extension module. It's an enormous amount of work for a bug that affects a minority of users, most of whom have a viable workaround.

您总是可以忽略对Py_Finalize的呼叫,第二次呼叫Py_Initialize是无人操作.这意味着您的应用程序在首次运行Python脚本时将使用额外的内存,并且直到退出您的内存才会返回给操作系统.只要您仍然不时地运行Python脚本,我就不会将其归类为泄漏.您的应用程序可能不是用Valgrind清洁的,但是比像筛子那样泄漏更好.

You can always just omit the call to Py_Finalize, calling Py_Initialize a second time is a no-op. This means your application will use additional memory usage when you first run a Python script, and that additional memory won't get returned to the OS until you exit. As long as you're still running Python scripts every once in a while, I wouldn't categorize it as a leak. Your application might not be Valgrind-clean, but it's better than leaking like a sieve.

如果您需要卸载(纯)Python模块以避免内存泄漏,则可以这样做.只需将它们从sys.modules中删除.

If you need to unload your (pure) Python modules to avoid leaking memory, you can do that. Just delete them from sys.modules.

Py_Finalize的缺点::如果要重复执行Python脚本,则在它们之间运行Py_Finalize并没有多大意义.每次重新初始化时,都必须重新加载所有模块.我的Python在启动时会加载28个模块.

Drawbacks of Py_Finalize: If you are executing Python scripts repeatedly, it doesn't make much sense to run Py_Finalize between them. You'll have to reload all the modules every time you reinitialize; my Python loads 28 modules at boot.

其他评论:该错误不仅限于Python.如果尝试卸载和重新加载库,大量任何语言的库代码都会泄漏内存.许多库都调用C代码,许多C程序员认为他们的库一次加载,而在进程退出时卸载.

Additional commentary: The bug is not limited to Python. A significant amount of the library code in any language will leak memory if you try to unload and reload libraries. Lots of libraries call into C code, lots of C programmers assume that their libraries gets loaded once and unloaded when the process exits.

这篇关于嵌入式Python 3解释器是否会泄漏内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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