roslyn icon indicating copy to clipboard operation
roslyn copied to clipboard

Make in memory assemblies in InteractiveAssemblyLoader can be collected when use scripting

Open zh6335901 opened this issue 1 year ago • 2 comments

See #31751 and #41722

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: image img_v3_02e5_36303847-acc1-4d97-b3e1-7f7b9f5b394g

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:

img_v3_02e5_e224f46c-e27f-44f2-ad41-8f2f31318b3g img_v3_02e5_1200b2b7-f776-4897-b0b3-6a5e5e8300dg

zh6335901 avatar Aug 27 '24 14:08 zh6335901

@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

zh6335901 avatar Aug 27 '24 15:08 zh6335901

Anyone can review it?

zh6335901 avatar Sep 05 '24 03:09 zh6335901

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!

DraconInteractive avatar Oct 27 '24 03:10 DraconInteractive

Could you please let me know if this fix is working and is available? We’re experiencing a production issue.

meetsekhar avatar Nov 01 '24 06:11 meetsekhar