1

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.

1 Answer 1

2

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 PyObjects (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.

Sign up to request clarification or add additional context in comments.

3 Comments

Is the PyObject created when you initialize python in C#? Also, you mentioned internally python objects count references to themselves, are they decreased/ deleted immediately by python's garbage collector when an object has 0 references?
Initializing python in C# creates many PyObject. Every object you receive from Python is also wrapped in PyObject.
As for deletion, as soon as there are 0 references to a Python object, it is deleted. There's a separate procedure in Python, that deletes reference cycles.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.