CSharpScript seemingly excessive memory usage
See original GitHub issueVersion Used: 2.3.0.0
I have an application that creates and pre-compiles a number of scripts. For me though, it seems that ~39 scripts is the limit where compilation will fail with an OOM exception.
var script = CSharpScript.Create(text, scriptOptions);
script.Compile();
My scriptOptions contains 1 assembly reference and 2 imports.
Each script compilation consumes roughly over 50mb.
Releasing the references to these scripts allows the gc to free up memory and not get an OOM ex meaning each script reference seems to consume roughly the same amount of memory as the entire main application without scripts. This seems slightly excessive.
I will help with any details needed. All help is welcome.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:3
- Comments:20
Top Results From Across the Web
CSharpScript memory leaks in ASP.NET core APi
The memory fall from more than 1 GB to 250 MG. I know, once an type or assembly is loaded, it can't be...
Read more >Compiling expression trees with Roslyn… without memory leaks
The problem is that this Roslyn magic comes with an unfortunate side effect. During compliation EvaluateAsync generates a new assembly in memory ......
Read more >intermediate – I Love C#
Roslyn C# script engine comes with a nifty class named CSharpScript which will do the trick for you. All we have to do...
Read more >Uncategorized – Page 2 – Shotgun Debugging
At a high level, our approach is going to be the following: Create an instance of CorDebugger , a debugger we can use...
Read more >https://raw.githubusercontent.com/dotnet/samples/m...
This notice is only meant for users making heavy use of these properties who are also actively working to reduce the memory allocations...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
As an update I found the probable cause for this, which unfortunately i did not add to the issue description:
The
globalsTypei was passing to the script is defined in the main assembly. Debugging the script compilation process I could make out that the reference manager seems to load all assemblies which are referenced in the assembly in which theglobalsTypeis located in.Moving the
globalsTypeto a separate assembly brought memory usage down to sane levels.I will leave this issue open for now.
To have less memory consumption you should use script.CreateDelegate()
var script = CSharpScript.Create<int>("X*Y", globalsType: typeof(Globals)); ScriptRunner<int> runner = script.CreateDelegate();The delegate doesn’t hold compilation resources (syntax trees, etc.) alive. https://github.com/dotnet/roslyn/wiki/Scripting-API-Samples#-create-a-delegate-to-a-script
Workaround: if you will use script.CreateDelegate(); and then call GC.Collect then your memory will be cleaned up.
To summarize - script.CreateDelegate() allocates a lot of memory but this memory will be collected during next GC.