[IronPython] Statefulness of CompiledCode

Korbinian Abenthum lists.ironpython.com at ka.weltenschmiede.com
Mon May 26 20:46:26 CEST 2008

Greg Parker wrote:
> I executed the same application in
> a tight loop about 1000 times.  The VM Size skyrocketed to
> ~500M (without the reload it stays at ~16M).  So this doesn't
> look like an option for me, especially since the actual script
> we are trying to run is much larger and more complicated.  As
> for changes to the C# code, in 1.1 if I try to clear the
> globals I just get an exception ('module' object has no
> attribute '__builtins__').  So am I better off just using the
> latest 2.0 build?

That has the same problem I'm afraid.
Apparently the memory is lost during the repeated compilation of 
SomeScript.py. If I compile it only once and do the import manually,

| ScriptEngine python = IronPython.Hosting.PythonEngine.CurrentEngine;
| CompiledCode compiledCode = python.CreateScriptSourceFromFile("Program.py").Compile();
| CompiledCode compiledCodeSomeInclude = python.CreateScriptSourceFromFile("SomeInclude.py").Compile();
| ScriptScope scope = python.CreateScope();
| for (int i = 0; i < 5000; i++)
| {
|     ScriptScope scopeSomeInclude = python.CreateScope();
|     compiledCodeSomeInclude.Execute(scopeSomeInclude);
|     python.Runtime.Globals.SetVariable("SomeInclude", scopeSomeInclude);
|     compiledCode.Execute(scope);
| }

the memory usage remains constant (and it's a whole lot faster).
I have no idea how that can be done in 1.1, or if it has side effects
I'm not aware of. And if you have complex import chains then this
solution might not translate very well to your problem in the first place.
HTH anyway.

More information about the Ironpython-users mailing list