[IronPython] Memory leak in PythonEngine.Evaluate ?

Szymon Kobalczyk s.kobalczyk at softwaremind.pl
Tue Aug 30 21:32:27 CEST 2005

First, big thanks to all of you for supporting this excellent product.

I was playing a bit with IP to see I it would work as embedded 
expression evaluator. The biggest requirement for such engine is to have 
stable memory consumption so I was trying to check how it looks with IP. 
I started with samples provided with the 9.1 release.

First I iterated the Embed1 sample 10 000 times. Unfortunately this 
resulted in big increase in memory allocation (from 10K to >100K 
confirmed both in Task Manager and with perfmon). I started looking at 
PythonEngine implementation and it looks that although I can create its 
instance all calls are redirected to a static global object. This 
doesn't "smells" nice.

Then I tried to see if I it make difference if I can precompile the 
expression and only evaluate it with different arguments/variables. SoI 
took and simplified some code from __builtin__ eval method.

        Parser p = Parser.fromString(script);
        Expr e = p.parseTest();

        Dictionary<object, object> globals = new Dictionary<object, 
        PythonModule mod = new PythonModule("<eval>", globals);
        object result = e.Evaluate(new NameEnv(mod, globals));

I checked this by also iterating 10 000 times and this time not only the 
memory allocation almost didnt change but also the code run much faster. 
So I wonder what happens in the PythonEngine that makes this huge memory 
loss? Another question is, why Expr.Evaluate() is marked as 
"experimental" and if it is safe to use it?

Szymon Kobalczyk.

More information about the Ironpython-users mailing list