[IronPython] Statefulness of CompiledCode

Dino Viehland dinov at exchange.microsoft.com
Tue May 27 18:06:04 CEST 2008

To avoid leaking memory in 1.x when repeatedly compiling modules you can enable the GenerateModulesAsSnippets option.  To do that programmatically you can just do IronPython.Compiler.Options.GenerateModulesAsSnippets = true;  It will then cause all modules to get compiled as dynamic methods but it will come with a (significant in v1.x, much less significant in 2.0) throughput cost.  You might also find that the compilation is still too expensive but at least memory won't be leaked anymore.

-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Korbinian Abenthum
Sent: Monday, May 26, 2008 11:46 AM
To: Discussion of IronPython
Subject: Re: [IronPython] Statefulness of CompiledCode

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.
Users mailing list
Users at lists.ironpython.com

More information about the Ironpython-users mailing list