Reputation: 897
I am trying to understand how memory is managed when python is embedded in C# using python.net. Python is executed in C# using the py.GIL
(global interpreter lock), essentially C# calls the python interpreter and executes the python code. What I don't understand is how is memory allocated and deallocated for python objects.
I couldn't find anything online and any type of help will be appreciated.
Upvotes: 1
Views: 728
Reputation: 3249
C# PyObject
is holding a Python object handle (which are pointers into C heap). Internally Python objects count references to themselves.
In C# you can either force releasing the handle by calling PyObject.Dispose
, in which case Python object refcount is decreased immediately, and if at this point it reaches 0, Python object will be destroyed and deallocated (from C heap).
Alternatively, wait until PyObject
instance is collected by .NET garbage collector. When that happens, the Python object handle is added to an internal queue, which is periodically emptied when you create new PyObject
s (e.g. PyObject
constructor will go check that queue, and release all handles in it).
This applies to Python.NET 2.5 and the upcoming 3.0.
Upvotes: 2