[IronPython] Is this memory leak ?

Szymon Kobalczyk s.kobalczyk at softwaremind.pl
Sat Nov 5 08:22:39 CET 2005


I tried doing this as you suggested and indeed now I have better 
results. But still after evaluating same script in a loop there is 
always one Frame object left in memory. It's being referenced by some 
obscure array of objects. The problem is that this Frame in turn 
references module which holds all the variables used in this 
computation. I quickly added some ClearAttrs method on module to prevent 
this.

So my thinking is that the memory problem is not only due to the 
compilation not using LCG (as this generates unmanaged memory leak that 
we also observed) but also that each time particular FrameCode is run 
the last Frame executed is stored in some static array.

I hope that you can hunt it down and fix somehow soon. For now we will 
try to precompile and cache our scripts when possible.

Martin Maly wrote:

>The x below is actually FrameCode (you can take a peek at __builtin__.cs, function compile to see how it comes to exist.
>
>There are 3 ways to compile (3rd parameter to compile):
>'exec' - supports series of statements:
>
>  
>
>>>>compile("x=2+2\nfor i in range(x):\n    print i\n", "", "exec")
>>>>        
>>>>
><code _5>
>  
>
>>>>eval(_)
>>>>        
>>>>
>0
>1
>2
>3
>
>'eval' - supports single expression
>'single' - one expression or statement
>
>That would be one way of doing things. Another could be somewhat simulating what IronPython does underneath, something similar to the following example that I quickly put together:
>
>using IronPython.Modules;
>using IronPython.Objects;
>using System.Collections.Generic;
>
>public class E {
>    public static void Main () {
>        IDictionary<object,object> d = new Dictionary<object,object>();
>        PythonModule eval = new PythonModule("<eval>", d);
>        FrameCode x = Builtin.compile("2+2", "<string>", "eval") as FrameCode;
>        for (;;) {
>            x.Run(new Frame(eval));
>        }
>    }
>}
>
>Martin
>
>-----Original Message-----
>From: Szymon Kobalczyk [mailto:s.kobalczyk at softwaremind.pl]
>Sent: Friday, November 04, 2005 2:57 PM
>To: Martin Maly
>Cc: users at lists.ironpython.com
>Subject: Re: [IronPython] Is this memory leak ?
>
>Sorry to bother you again.
>
>  
>
>>x = compile("2+2", "<string>", "eval")
>>for i in xrange(123456789):
>>   eval(x)
>>
>>
>>
>>    
>>
>In this code, what object is assigned to the x var? Can I just store it by calling PythonEngine.Evaluate once than invoke multiple times passing it as variable:
>
>PythonEngine engine = new PythonEngine(); object x = engine.Evaluate("compile('2+2', '<string>', 'eval')");
>
>and somewhere else
>
>PythonEngine engine2 = new PythonEngine(); engine2.SerVariable("x", x); object result = engine.Evaluate("eval(x)");
>
>Besides, my scripts are not always simple expressions. Sometimes there are multiple statements and thats why I mostly use PythonEngine.Execute method. Would the compile function also handle such case? (I guess the kind argument would should be 'exec' then)
>
>
>Szymon.
>
>  
>





More information about the Ironpython-users mailing list