matplotlib-cpp icon indicating copy to clipboard operation
matplotlib-cpp copied to clipboard

How to release the memory?

Open chengts95 opened this issue 6 years ago • 3 comments

When I call the plot function the memory usage goes up a lot and I do not know how to recycle it.

chengts95 avatar Jun 10 '19 03:06 chengts95

In theory the python objects are ref-counted and should be correctly destroyed once they leave scope. In practice it's entirely possible that there's a bug somewhere that's leaking references, in that case your only remedy would be to debug the issue (and to submit a PR with the fix afterwards). There's no specific function to manually release memory.

lava avatar Aug 12 '19 12:08 lava

I can confirm there is still a massive memory leak (~5 MB per plot; probably the size of the data to plot) if you loop plots, even if you close them.

This is because if Py_Finalize() is not called, data stays in memory even if it is not displayed. The problem is that if you call Py_Finalize() then you cannot call Py_Initialize() again, basically it is only allowed to call Py_Initialize() and Py_Finalize() once in a program.

davsar89 avatar Aug 01 '24 07:08 davsar89

Found a solution: force call the Python Garbage collector from time to time (or after each plot)

PyRun_SimpleString("import gc; gc.collect()");

davsar89 avatar Aug 01 '24 09:08 davsar89