Python 3 解释器嵌入时会泄漏内存吗?

发布于 2024-12-26 01:12:42 字数 495 浏览 1 评论 0原文

此错误报告指出,截至 2007 年 6 月,Python 解释器在调用具有嵌入式 Python 解释器的 C/C++ 应用程序中的 Py_Finalize。建议在应用程序终止时调用 Py_Finalize 一次。

此错误报告指出,截至版本 3.3 和 2011 年 3 月,解释器仍然存在内存泄漏问题。

有谁知道这个问题的现状?我很担心,因为我有一个应用程序,其中每个运行实例都会多次调用解释器,并且我遇到了内存泄漏。

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

这是一个容易处理的问题还是 Python 解释器中的一个错误?

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.

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.

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木有鱼丸 2025-01-02 01:12:42

您可以看到该错误(第一个错误,从 2007 年开始)已被 nnorwitz 作为“wontfix”关闭,并且他的帖子位于错误报告中。

为什么多次调用Py_Initialize/Py_Finalize
为什么不做这样的事情(我有点混合 C 和
Python 为方便起见):

/* 启动 */
Py_Initialize();

/* 做任何事 */
while (moreFiles()) {
    PyRun_SimpleString("execfile('%s')" % nextFile());
    /* 做任何事 */
}

/* 关闭 */
Py_Finalize();

问题是大多数编写 Python 模块的人并不担心他们的模块被终结并重新初始化会发生什么,并且通常不关心终结期间的清理。模块作者知道,当进程退出时,所有内存都会被释放,并且不需要担心更多的事情。

所以这并不是一个真正的错误,而是一千个错误——每个扩展模块一个。对于影响少数用户的错误来说,这是一项艰巨的工作,其中大多数用户都有可行的解决方法。

您始终可以省略对 Py_Finalize 的调用,第二次调用 Py_Initialize 是无操作。这意味着当您第一次运行 Python 脚本时,您的应用程序将使用额外的内存,并且在您退出之前,这些额外的内存不会返回到操作系统。只要您仍然偶尔运行 Python 脚本,我就不会将其归类为泄漏。您的应用程序可能不是 Valgrind 干净的,但它比像筛子一样泄漏要好。

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

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

附加评论:该错误不仅限于Python。如果您尝试卸载并重新加载库,任何语言中的大量库代码都会泄漏内存。许多库调用 C 代码,许多 C 程序员假设他们的库被加载一次并在进程退出时卸载。

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.

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();

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.

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.

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

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文