Make in memory assemblies in InteractiveAssemblyLoader can be collected when use scripting
Add isCollectible parameter to InteractiveAssemblyLoader makes collecting assemblies that in memory possible.
My test code:
while (true)
{
var loader = new InteractiveAssemblyLoader(isCollectible: true);
var options = ScriptOptions.Default
.AddReferences(MetadataReference.CreateFromFile(typeof(TestClass).Assembly.Location))
.WithImports("System", "ScriptingClassLibrary");
var script = CSharpScript.Create("new TestClass().ToJson(1)", options, assemblyLoader: loader);
var result = await script.RunAsync().ConfigureAwait(false);
result = await result.ContinueWithAsync("1 + 2").ConfigureAwait(false);
result = await result.ContinueWithAsync("new TestClass().ToJson(\"aaa\")").ConfigureAwait(false);
result = await result.ContinueWithAsync("new TestClass().ToJson(false)").ConfigureAwait(false);
result = await result.ContinueWithAsync("1 + 3").ConfigureAwait(false);
loader.Dispose();
GC.Collect();
GC.WaitForPendingFinalizers();
await Task.Delay(100).ConfigureAwait(false);
var currentProcess = Process.GetCurrentProcess();
Console.WriteLine($"Process handle count: {currentProcess.HandleCount}");
}
Test result:
You can see memory usage and process handle count is steady.
But same test code(without isCollectible parameter) use Microsoft.CodeAnalysis.CSharp.Scripting 4.12.0-1.final package:
@zh6335901 please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
@dotnet-policy-service agree [company="{your company}"]Options:
- (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@dotnet-policy-service agree
- (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@dotnet-policy-service agree company="Microsoft"Contributor License Agreement
@dotnet-policy-service agree
Anyone can review it?
Curious to whether this is still applicable? Running into the problem of assemblies hanging around in memory on the latest branch of ros, this seems like a solid solution to me. Hope it gets bumped for review!
Could you please let me know if this fix is working and is available? We’re experiencing a production issue.