CSharpScript.EvaluateAsync() growing memory usage

See original GitHub issue

Version Used: 2.10.0

Steps to Reproduce:

It’s quite easy to reproduce, just run in Loop the following code:

string formula = "Math.Round((double)(15/(double)10*100),2)"; object result = await CSharpScript.EvaluateAsync(formula, o_scriptoptions);

Expected Behavior: I was not expecting an ever-growing memory usage.

Actual Behavior:

The behaviour is well described by the following pictures from our memory profiler, showing live instances and bytes:

image

image

The issue might be related to the following: #22219 #10164

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:7
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

9reactions
MaxiPignacommented, Sep 11, 2020

Any update to share on this issue?

1reaction
rummelsworthcommented, Mar 2, 2020

This hit an app I’m working on pretty hard. We run long “analysis” operations and were trying to run hundreds at once when we ran into this. Our expressions are numeric-only and simple enough that we easily transitioned to Jace.NET to avoid this memory leak in Roslyn, but we’ve got another app in which expressions can have string parameters and operations, which will prevent usage of Jace.NET.

I was about to create a new issue for this, so I’ll add what I had already written:

Version Used: Microsoft.CodeAnalysis.CSharp.Scripting 3.4.0. Also tried 3.5.0-beta3-final.

Steps to Reproduce:

  1. Create a new .NET Core 3.1 console app.

  2. Install the Microsoft.CodeAnalysis.CSharp.Scripting NuGet package.

  3. Replace the contents of Program.cs with this:

    using Microsoft.CodeAnalysis.CSharp.Scripting;
    using System;
    
    namespace ConsoleApp1
    {
        internal class Program
        {
            private static void Main(string[] args)
            {
                Once();
                Once();
            }
    
            private static void Once()
            {
                Console.WriteLine();
                GC.Collect();
                Console.WriteLine("before: " + GC.GetTotalMemory(true));
    
                const string EXPRESSION = "123";
                int result;
    
                using (var task = CSharpScript.EvaluateAsync<int>(EXPRESSION))
                {
                    result = task.Result;
                }
    
                Console.WriteLine("  " + result);
                GC.Collect();
                Console.WriteLine("after: " + GC.GetTotalMemory(true));
            }
        }
    }
    
  4. Set breakpoints at line 11 (after the first Once invocation) and 12 (after the second Once invocation).

  5. Run the app under Debug.

  6. When the first breakpoint hits, ensure the Diagnostic Tools window is open, and take a memory usage snapshot.

  7. Let it continue to the next breakpoint, and then take another memory snapshot.

Expected Behavior:

No increase in allocated objects/memory.

Actual Behavior:

Some increase in allocated objects/memory:

image

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to avoid memory leak using Roslyn CSharpCompilation
I am using Roslyn in my project to dynamically compile scripts. I noticed that the memory usage of my application was astronomically high(20- ......
Read more >
[Solved]-How to use Roslyn C# scripting in batch processing ...
it reuse the script<object> - it's not consuming more memory on second call to runasync . but we can do better: static void...
Read more >
Compiling expression trees with Roslyn… without memory leaks
The memory leak problem​​ The problem is that this Roslyn magic comes with an unfortunate side effect. During compliation EvaluateAsync generates ...
Read more >
Best way to evaluate expressions dynamically in .NET?
I'm stuck at the part where I need to evaluate the expression in (2). ... I actually implemented it with Roslyn, but the...
Read more >
my c# application usage memory is keep increasing every ...
This is absolutely unreliable method of observing the memory usage. Just disregard it. Some thing that it's impossible to have a leak of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found