简体   繁体   English

将 python 嵌入我的应用程序时出现内存泄漏

[英]Memory leak when embedding python into my application

The following program, when linked against python 2.7.13 and run on Windows 10 slowly but steadily leaks memory.以下程序在与 python 2.7.13 链接并在 Windows 10 上缓慢但稳定地运行时会泄漏内存。

#include <Python.h>
#include <iostream>

int main()
{
    std::cout << "Python version: " << PY_VERSION << std::endl;

    while (true)
    {
        Py_Initialize();
        //PyGC_Collect();
        Py_Finalize();
    }

    return 0;
}

The interesting fact is that it seems not every iteration leaks memory.有趣的事实是,似乎并非每次迭代都会泄漏内存。 What I see, though, is that the reference count that python prints slowly increases by a (non-constant) count of approximately 90 per iteration regardless of the leak.但是,我看到的是,无论泄漏如何,python 打印的引用计数都会缓慢增加(非常量)每次迭代大约 90 次。 Using the Visual Studio Diagnostic Tools I figured out that the leak is coming from a call to PyImport_ImportModule() when it reads a compiled module from disk (the actual call stack is several levels deep).使用 Visual Studio 诊断工具,我发现泄漏来自PyImport_ImportModule()从磁盘读取编译模块时的调用(实际调用堆栈有几层深)。

Are any additional cleanup steps necessary that I am not aware of?是否需要任何我不知道的额外清理步骤? Or is there something about the Python garbage collector that might cause this and it is not a "real" memory leak?或者是否有关于 Python 垃圾收集器的某些内容可能导致这种情况,并且它不是“真正的”内存泄漏?

Py_Finalize — Python/C API Reference Manual (emphasis mine): Py_Finalize — Python/C API 参考手册(重点是我的):

<...> <...>
Bugs and caveats: The destruction of modules and objects in modules is done in random order;错误和警告:模块中的模块和对象的销毁是按随机顺序进行的; this may cause destructors (__del__() methods) to fail when they depend on other objects (even functions) or modules.这可能会导致析构函数(__del__() 方法)在依赖于其他对象(甚至函数)或模块时失败。 Dynamically loaded extension modules loaded by Python are not unloaded.不会卸载 Python 加载的动态加载的扩展模块。 Small amounts of memory allocated by the Python interpreter may not be freed (if you find a leak, please report it). Python解释器分配的少量内存可能无法释放(如果发现泄漏,请报告)。 Memory tied up in circular references between objects is not freed.对象之间的循环引用所占用的内存不会被释放。 Some memory allocated by extension modules may not be freed.一些由扩展模块分配的内存可能不会被释放。 Some extensions may not work properly if their initialization routine is called more than once;如果多次调用初始化例程,某些扩展可能无法正常工作; this can happen if an application calls Py_Initialize() and Py_Finalize() more than once.如果应用程序多次调用 Py_Initialize() 和 Py_Finalize() 就会发生这种情况。

Above answer puts it all together上面的答案把它们放在一起

#include <Python.h>


int main(int argc, char *argv[])
{
    Py_Initialize();
    wchar_t *name = Py_DecodeLocale(argv[0], NULL);
    Py_SetProgramName(name); 
    
    pythonlovesC();

    Py_Finalize();
    return 0;
}

void pythonlovesC()
{

    while (true)
    {
       // do your python stuff here  
       PyGC_Collect();
        
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM