[Python.NET] Memory leak problem in function "AsManagedObject"

Eliana Mendes eliana.mendesp at gmail.com
Mon Jan 18 14:11:26 EST 2016


Hello Tony,



Thanks a lot for your answer! I actually just got directly the
“Python.Runtime.dll”  from the download link in sourceforge:
http://sourceforge.net/projects/pythonnet/files/. But from the description
I see that the last update of that one was done in 2013. Would there be a
newer version somewhere else that I could get?  Perhaps this version I’m
using is out of date and you have a newer one. Let me know if that’s the
case.



I tried your suggestion of putting the “using” statement for the tuple
objects, and also for “myTuple”, and the memory is still increasing a lot.
I’ve made a memory profile to see which objects are being retained and
after I ran that statement 5 thousand times I got this:


[image: Imagem inline 1]


As you can see more that 12 thousand instances of “PyInt” and more than 6
thousand of “PyObject” are created . Apparently it is creating these
“PyInt” and “PyObjects” somewhere inside the “AsManagedObject”, because I
don’t have PyInt objects anywhere in my code and this just happens when I
call “AsManagedObject”.



Let me know if there is a newer version that could probably have that
problem fixed which I am not aware of.


I appreciate a lot your help!



Best regards,

Eliana

2016-01-17 6:05 GMT-06:00 Tony Roberts <tony at pyxll.com>:

> Hi Eliana,
>
> which version of pythonnet are you using? when you say you're using the
> latest are you building it yourself from the develop branch on github?
>
> I had a quick look at the code that converts from a python object to a
> double, and I don't see any obvious memory leaks there. Perhaps the leak is
> the objects resulting from indexing into the tuple are never getting
> disposed? Try something like this instead and see if it helps (and report
> back as it may help improve the code if we know exactly what the problem
> is).
>
> PyTuple myTuple = PyTuple.AsTuple(result);
>
> double result0;
> using (var item0 = myTuple[0])
>     result0 = (double)item0.AsManagedObject(typeof(double));
>
> double result1;
> using (var item1 = myTuple[1])
>     result1 = (double)item1.AsManagedObject(typeof(double));
>
> myTuple.Dispose();
>
> I would also use a using statement for the myTuple as well, just to be
> sure dispose is called in the case an exception is thrown somewhere.
>
> Regards,
> Tony
>
>
> On Fri, Jan 15, 2016 at 8:06 PM Eliana Mendes <eliana.mendesp at gmail.com>
> wrote:
>
>> Hello experts,
>>
>>
>>
>> I'm having a memory leak problem when using the
>> function AsManagedObject(typeof(double)). Basically I have something like
>> this:
>>
>>
>>
>> PyTuple myTuple = PyTuple.AsTuple(result);
>>
>> double result0 = (double)myTuple[0].AsManagedObject(typeof(double));
>>
>> double result1 = (double)myTuple[1].AsManagedObject(typeof(double));
>>
>> myTuple.Dispose();
>>
>>
>>
>> where "result" is just a PyObject that returned from a python function. I
>> simplified the code above just so you can understand better, but the thing
>> is that the line that calls "AsManagedObject”  is executed thousands of
>> times and it is increasing significantly the memory heap (it goes over 3 GB
>> of memory in my scenario and it’s not released after execution). If I don't
>> call just this specific function the memory remains stable. But I don’t
>> know any other way to convert the PyObject to "double" unless using the
>> “AsManagedObject” function.
>>
>> It sounds to me that some objects are allocated inside the
>> "AsManagedObject" method and they are not being released. Maybe it’s a bug
>> there. Any ideas? I'm using latest version of python for .NET.
>>
>>
>>
>> Thank you!
>>
>>
>> Eliana Mendes
>>
>> Software Engineer
>>
>> _________________________________________________
>> Python.NET mailing list - PythonDotNet at python.org
>> https://mail.python.org/mailman/listinfo/pythondotnet
>
>
> _________________________________________________
> Python.NET mailing list - PythonDotNet at python.org
> https://mail.python.org/mailman/listinfo/pythondotnet
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pythondotnet/attachments/20160118/46b58f11/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 70883 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/pythondotnet/attachments/20160118/46b58f11/attachment-0001.png>


More information about the PythonDotNet mailing list