You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Sep 14, 2018. It is now read-only.
It seems that PythonContext objects created under the hood in hosted scripting can leak in some circumstances. From my observations, it seems normal and acceptable that the first instance of PythonContext remains live, and the same applies to the latest one.
Other instances, however, should be collected. This is important because PythonContext holds references to AST data that can get pretty large (I have observed 70 MB leaks at work).
The example below shows how to create leaks. Importing some modules triggers these leaks, but not all. For instance, sys is OK, but os leaks.
using System;using System.Collections.Generic;using System.Threading;using Microsoft.Scripting.Runtime;using IronPython.Compiler;using IronPython.Runtime;using Microsoft.Scripting.Hosting.Providers;using Microsoft.Scripting.Hosting;namespaceIronPythonLeak{classProgram{staticreadonlystringcode=@"import os";staticWeakReferencewr=null;staticvoidAddSearchPath(ScriptEngineengine){varsp= engine.GetSearchPaths();// Set to your liking
sp.Add(@"....\main\External.LCA_RESTRICTED\Languages\IronPython\27\Lib");
engine.SetSearchPaths(sp);}staticvoidRunOnce(boolstoreLanguageContext){varoptions=newDictionary<string,object>();
options["Frames"]= ScriptingRuntimeHelpers.True;
options["FullFrames"]= ScriptingRuntimeHelpers.True;
options["LightweightScopes"]= ScriptingRuntimeHelpers.True;varsetup= IronPython.Hosting.Python.CreateLanguageSetup(options);
setup.ExceptionDetail =true;varruntime= IronPython.Hosting.Python.CreateRuntime();varengine= runtime.GetEngineByFileExtension("py");if(storeLanguageContext)wr=new WeakReference(HostingHelpers.GetLanguageContext(engine));varcompilerOptions=(PythonCompilerOptions)engine.GetCompilerOptions();
compilerOptions.Module |= ModuleOptions.Interpret;
compilerOptions.Module &= ~ModuleOptions.Optimized;
AddSearchPath(engine);// Needed if using freshly compiled, non-installed IronPython DLLs.
engine.Execute(code);
runtime.Shutdown();}staticvoidMain(string[]args){
Console.Write(code);
RunOnce(false);
RunOnce(true);
RunOnce(false);
System.GC.Collect();if(wr.IsAlive)
Console.Write("Leaked");else
Console.Write("Not leaked");
Console.ReadKey(true);}}}
The text was updated successfully, but these errors were encountered:
It seems that PythonContext objects created under the hood in hosted scripting can leak in some circumstances. From my observations, it seems normal and acceptable that the first instance of PythonContext remains live, and the same applies to the latest one.
Other instances, however, should be collected. This is important because PythonContext holds references to AST data that can get pretty large (I have observed 70 MB leaks at work).
The example below shows how to create leaks. Importing some modules triggers these leaks, but not all. For instance, sys is OK, but os leaks.
The text was updated successfully, but these errors were encountered: