Thank you. I am fine with a little delay before the objects are collected, I 
just wanted to make sure there are no other side effects by not calling 
Dispose() on the objects.

On a side node, regarding GC.AddMemoryPressure: PyObjects have some (possibly 
large) amount of backing unmanaged memory. The .NET garbage collector does not 
know about this memory and thus cannot take it into account when deciding when 
the GC should be triggered. AddMemoryPressure allows you to tell the .NET GC 
about the unmanaged backing memory. However, PythonNET may not know the amount 
of memory that has been allocated when constructing python objects.

Best regards,
Thomas Lundgaard Hansen

From: PythonDotNet 
<pythondotnet-bounces+thomas.lundgaard=3shape....@python.org> On Behalf Of Li 
Amos
Sent: Sunday, September 8, 2019 8:12 PM
To: A list for users and developers of Python for .NET <pythondotnet@python.org>
Subject: Re: [Python.NET] Memory management when embedding python.

If you want to dispose the PyObject and reduce the reference count immediately 
you should call `Dispose` manually or use `using` statement.
Otherwise, GC will take care of it although it would be a little delay, you can 
adjust ` Finalizer.Instance.Threshold ` for reducing the delay, it refers how 
many PyObjects will be collect at once after GC mark them as garbage.

About `GC.AddMemoryPressure`, there's no relate with it, PyObject only contains 
pointers of CPython, it doesn't allocate any unmanaged memory, thus GC know the 
objects' size.

From: Denis Akhiyarov<mailto:denis.akhiya...@gmail.com>
Sent: Sunday, September 8, 2019 23:43
To: A list for users and developers of Python for 
.NET<mailto:pythondotnet@python.org>
Subject: Re: [Python.NET] Memory management when embedding python.

There is a lot more going on this topic of GC in GitHub repo, but I'm not 
involved with the project anymore.

Anyway if anyone is interested in moderating this mailing list, please let me 
know.

Thanks,
Denis

On Sun, Sep 8, 2019, 10:35 AM Thomas Lundgaard Hansen 
<thomas.lundga...@3shape.com<mailto:thomas.lundga...@3shape.com>> wrote:
Hi.

I am embedding Python into a .NET (C#) application. For the purpose of this 
discussion my code is
like the example given here:
https://github.com/pythonnet/pythonnet#example
(except that I am working with arrays of a substantial size).

Each of the "dynamic" variables created in that example are of the .NET type 
PyObject, which is
a disposable type. The code in the example does not call Dispose() on these 
objects and it would
indeed be very inconvenient to do so.

Whats the best practice here and what considerations went into this design? Can 
we expect that
the only unmanged resources held by PyObject's are unmanaged memory? (Unless, 
of course, the
PyObject represents a system resource like a file or network stream).

Has it been considered to use GC.AddMemoryPressure when PyObjects are backed by 
a large
amount of unmanaged memory?


Thanks,
Thomas Lundgaard Hansen

_________________________________________________
Python.NET mailing list - 
PythonDotNet@python.org<mailto:PythonDotNet@python.org>
https://mail.python.org/mailman/listinfo/pythondotnet

_________________________________________________
Python.NET mailing list - PythonDotNet@python.org
https://mail.python.org/mailman/listinfo/pythondotnet

Reply via email to