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

Tony Roberts tony at pyxll.com
Sun Jan 17 07:05:29 EST 2016


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pythondotnet/attachments/20160117/93d502dc/attachment.html>


More information about the PythonDotNet mailing list