Open leviska opened 2 weeks ago
If you call PyEval_EvalCode, memory will be cleared if locals != globals, but will accumulate if locals == globals (or locals == Py_None, which is the same)
PyEval_EvalCode
locals != globals
locals == globals
locals == Py_None
The code sample:
The output of the given code on my machine:
# globals=dict, locals=dict 66.421875 136.5625 206.72265625 276.87890625 347.03515625 136.54296875 206.68359375 276.83203125 346.98046875 417.12890625
Yet, if we call PyEval_EvalCode(compiled_code, dict, dict2); (dict2, not dict), then the output becomes
PyEval_EvalCode(compiled_code, dict, dict2);
# globals=dict, locals=dict2 66.421875 66.3046875 86.34765625 86.36328125 86.36328125 86.3671875 86.3671875 86.3671875 86.3671875 86.3671875
Adding
import gc gc.collect()
At the end of the python code seems to help even in the first case, but still the memory can be larger:
# globals=dict, locals=dict, add gc 66.2109375 136.46875 136.375 136.37890625 156.3125 156.2734375 156.3515625 156.2421875 156.2734375 156.2421875
Originally I found this using pyo3 library in Rust (I have a MRE in Rust too), so I'm pretty sure, that it's not related to my poor C code.
3.11
Linux
I'm not sure that this is a bug, but I have absolutely no clue why the behavior is different
You can find repo with cmakefile and rust variant here https://github.com/leviska/cpython_mem_leak
Bug report
Bug description:
If you call
PyEval_EvalCode
, memory will be cleared iflocals != globals
, but will accumulate iflocals == globals
(orlocals == Py_None
, which is the same)The code sample:
Code sample
```c++ #includeThe output of the given code on my machine:
Yet, if we call
PyEval_EvalCode(compiled_code, dict, dict2);
(dict2, not dict), then the output becomesAdding
At the end of the python code seems to help even in the first case, but still the memory can be larger:
Originally I found this using pyo3 library in Rust (I have a MRE in Rust too), so I'm pretty sure, that it's not related to my poor C code.
CPython versions tested on:
3.11
Operating systems tested on:
Linux