[IronPython] Memory Leak in IronPython 2.6 RC3

Dino Viehland dinov at microsoft.com
Wed Nov 25 14:37:36 CET 2009


My guess here (I'm on vacation so I haven't tried this) is that some exception info 
is not getting cleared - 

ExceptionHelpers.DynamicStackFrames = null; 

In the catch block will probably fix it although we should probably
do this ourselves.

> -----Original Message-----
> From: users-bounces at lists.ironpython.com [mailto:users-
> bounces at lists.ironpython.com] On Behalf Of Jonathan Howard
> Sent: Tuesday, November 24, 2009 4:45 PM
> To: users at lists.ironpython.com
> Cc: jfelkner at drawloop.com
> Subject: [IronPython] Memory Leak in IronPython 2.6 RC3
> 
> I'm trying to track down a memory leak in our hosted IronPython
> application as we upgrade to 2.6 from 1.1.2.  I saw a post at
> stackoverflow ( http://stackoverflow.com/questions/1664567/embedded-
> ironpython-memory-leak
> ) showing how to set up the environment to avoid leaking memory, but
> we're still having a memory leak.  If I take our identical setup code,
> and use it on very simple code, there's no problem, but we have
> thousands of lines of Python at this point.
> 
> Below is a minimum way to introduce a memory leak inside a hosted
> IronPython application.  I don't know if it's the only way, or if it's
> what's affecting us, but it does cause a leak:  (Obviously it needs
> the appropriate DLLs etc.)
> 
> #############################################
> 
> using System;
> using System.Threading;
> using IronPython.Hosting;
> using IronPython.Runtime;
> using IronPython.Compiler;
> using System.Collections.Generic;
> using Microsoft.Scripting.Hosting;
> 
> namespace IPyTest
> {
>     class Program
>     {
>         static void Main(string[] args)
>         {
>             bool cont = true;
>             while (cont)
>             {
>                 var ipy = new IPy();
>                 try
>                 {
>                     // Set the below boolean to "false" to run without
> a memory leak
>                     // Set it to "true" to run with a memory leak.
>                     ipy.run(true);
>                 }
>                 catch { }
>             }
>         }
>     }
> 
>     class IPy
>     {
>         private string scriptWithoutLeak = "import random;
> random.randint(1,10)";
>         private string scriptWithLeak = "raise Exception(), 'error'";
> 
>         public IPy()
>         {
>         }
> 
>         public void run(bool withLeak)
>         {
>             //set up script environment
>             Dictionary<String, Object> options = new
> Dictionary<string, object>();
>             options["LightweightScopes"] = true;
>             ScriptEngine engine = Python.CreateEngine(options);
>             PythonCompilerOptions pco = (PythonCompilerOptions)
> engine.GetCompilerOptions();
>             pco.Module &= ~ModuleOptions.Optimized;
>             engine.SetSearchPaths(new string[]{
>                 @"C:\Program Files\IronPython 2.6\Lib"
>             });
>             ScriptRuntime runtime = engine.Runtime;
>             ScriptScope scope = runtime.CreateScope();
>             var source = engine.CreateScriptSourceFromString(
>                 withLeak ? scriptWithLeak : scriptWithoutLeak
>             );
>             var comped = source.Compile();
>             comped.Execute(scope);
>             runtime.Shutdown();
>         }
>     }
> }
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com



More information about the Ironpython-users mailing list