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
[email protected]
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com