Thank you all for the help. I found the problem is resolved if call PyGC_Collect() after PyDict_DelItemString(). Is it expected to call PyGC_Collect() here for Python 3.5 which is not needed for Python 3.3?
2016-09-20 16:01 GMT+08:00 Chris Angelico <ros...@gmail.com>: > On Tue, Sep 20, 2016 at 4:19 PM, dl l <ldlch...@gmail.com> wrote: > > Yes, it's a workaround to set the global variables to None. But My app > just > > provide a framework for my customers to run python scripts. That's means > > the workaround requires my customers to update their python scripts. That > > may make them unhappy :(. Is there a workaround in my code C++ side to > call > > Python C APIs to resolve the memory leak issue? > > > > Python's import system is fundamentally not designed for what I > believe is your use-case here, of running potentially-edited user > code. Instead, I suggest keeping 'import' for stable modules that > don't get updated without restarting the process, and for the more > dynamic code, build something using exec (or PyRun_StringFlags, which > I believe is the C equivalent). Object lifetimes will then be entirely > under your control. > > ChrisA > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list