<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:#954F72;
        text-decoration:underline;}
p.msonormal0, li.msonormal0, div.msonormal0
        {mso-style-name:msonormal;
        mso-margin-top-alt:auto;
        margin-right:0in;
        mso-margin-bottom-alt:auto;
        margin-left:0in;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
span.EmailStyle18
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:#404040;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.25in 1.0in 1.25in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="DA" link="blue" vlink="#954F72">
<div class="WordSection1">
<p class="MsoNormal"><span lang="EN-US" style="color:#404040;mso-fareast-language:EN-US">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.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="color:#404040;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="color:#404040;mso-fareast-language:EN-US">On a side node, regarding
</span><span lang="EN-US">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.</span><span lang="EN-US" style="color:#404040;mso-fareast-language:EN-US"><o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US" style="color:#404040;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<div>
<p class="MsoNormal"><span lang="EN-US" style="color:#404040">Best regards, <o:p>
</o:p></span></p>
<p class="MsoNormal" style="text-align:justify"><a name="_Hlk490731213"></a><a name="_Hlk494878765"><span style="mso-bookmark:_Hlk490731213"><b><span lang="EN-US" style="color:#404040">Thomas Lundgaard Hansen</span></b></span></a><span style="mso-bookmark:_Hlk494878765"><span style="mso-bookmark:_Hlk490731213"><b><span lang="EN-US" style="color:#404040"><o:p></o:p></span></b></span></span></p>
</div>
<span style="mso-bookmark:_Hlk490731213"></span><span style="mso-bookmark:_Hlk494878765"></span>
<p class="MsoNormal"><span lang="EN-US" style="color:#404040;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span lang="EN-US">From:</span></b><span lang="EN-US"> PythonDotNet <pythondotnet-bounces+thomas.lundgaard=3shape.com@python.org>
<b>On Behalf Of </b>Li Amos<br>
<b>Sent:</b> Sunday, September 8, 2019 8:12 PM<br>
<b>To:</b> A list for users and developers of Python for .NET <pythondotnet@python.org><br>
<b>Subject:</b> Re: [Python.NET] Memory management when embedding python.<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">If you want to dispose the PyObject and reduce the reference count immediately you should call `Dispose` manually or use `using` statement.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">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.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">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.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span lang="EN-US">From: </span></b><span lang="EN-US"><a href="mailto:denis.akhiyarov@gmail.com">Denis Akhiyarov</a><br>
<b>Sent: </b>Sunday, September 8, 2019 23:43<br>
<b>To: </b><a href="mailto:pythondotnet@python.org">A list for users and developers of Python for .NET</a><br>
<b>Subject: </b>Re: [Python.NET] Memory management when embedding python.<o:p></o:p></span></p>
</div>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">There is a lot more going on this topic of GC in GitHub repo, but I'm not involved with the project anymore.
<o:p></o:p></span></p>
<div>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US">Anyway if anyone is interested in moderating this mailing list, please let me know.<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US">Thanks,<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span lang="EN-US">Denis<o:p></o:p></span></p>
</div>
<p class="MsoNormal" style="margin-bottom:12.0pt"><span lang="EN-US"><o:p> </o:p></span></p>
<div>
<div>
<p class="MsoNormal"><span lang="EN-US">On Sun, Sep 8, 2019, 10:35 AM Thomas Lundgaard Hansen <<a href="mailto:Thomas.Lundgaard@3shape.com">Thomas.Lundgaard@3shape.com</a>> wrote:<o:p></o:p></span></p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-top:5.0pt;margin-right:0in;margin-bottom:5.0pt">
<div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="color:#404040">Hi.</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span style="color:#404040"> </span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US" style="color:#404040">I am embedding Python into a .NET (C#) application. For the purpose of this discussion my code is</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US" style="color:#404040">like the example given here:</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><a href="https://github.com/pythonnet/pythonnet#example" target="_blank"><span lang="EN-US">https://github.com/pythonnet/pythonnet#example</span></a><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US">(except that I am working with arrays of a substantial size).</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US"> </span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US">Each of the “dynamic” variables created in that example are of the .NET type PyObject, which is</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US">a disposable type. The code in the example does not call Dispose() on these objects and it would</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US">indeed be very inconvenient to do so.</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US"> </span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US">Whats the best practice here and what considerations went into this design? Can we expect that</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US">the only unmanged resources held by PyObject’s are unmanaged memory? (Unless, of course, the</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US">PyObject represents a system resource like a file or network stream).</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US"> </span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US">Has it been considered to use GC.AddMemoryPressure when PyObjects are backed by a large</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US">amount of unmanaged memory?</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US" style="color:#404040"> </span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US" style="color:#404040"> </span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-align:justify">
<span lang="EN-US" style="color:#404040">Thanks,</span><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;text-align:justify">
<a name="m_6284924964469645628__Hlk490639683"><b><span lang="EN-US" style="color:#404040">Thomas Lundgaard Hansen</span></b></a><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span lang="EN-US"> </span><o:p></o:p></p>
</div>
</div>
</blockquote>
</div>
<p class="MsoNormal" style="margin-left:4.8pt"><span lang="EN-US">_________________________________________________<br>
Python.NET mailing list - <a href="mailto:PythonDotNet@python.org" target="_blank">
PythonDotNet@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/pythondotnet" target="_blank">https://mail.python.org/mailman/listinfo/pythondotnet</a><o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
</div>
</body>
</html>