简体   繁体   English

ASP.NET核心APi中的CSharpScript内存泄漏

[英]CSharpScript memory leaks in ASP.NET core APi

I have an asp core api. 我有一个ASP核心API。 A route can execute several c# scripts from database to get some calcul results on the same context/globals. 一条路由可以从数据库中执行几个c#脚本,以在相同的上下文/全局变量上获得一些计算结果。

So I have this code: 所以我有这段代码:

    public static async Task<FormulaEvalException> TryEvalAsync<T>(this T formulaContext) where T : FormulaContext
    {
        FormulaEvalException res = null;
        ScriptState state = null;
        var scriptOptions = ScriptOptions.Default.WithReferences("System", "System.Linq", "System.Globalization", "Microsoft.CSharp").WithImports(new[] { "System", "System.Linq", "System.Math", "System.Globalization", "System.Collections.Generic" });
        foreach (var formulaList in formulaContext.AllFormulas.Values)
        {
            foreach (var formula in formulaList)
            {
                formulaContext.CurrentFormula = formula;
                try
                {
                    if (state == null)
                    {
                        state = await CSharpScript.RunAsync(formula.Script, scriptOptions, formulaContext);
                    }
                    else
                    {
                        state = await state.ContinueWithAsync(formula.Script);
                    }
                    var result = state.ReturnValue;
                    if (result == null)
                    {
                        if (res == null)
                        {
                            res = new FormulaEvalException(formula.Title + " : No result");
                        }
                        continue;
                    }

                    formula.Result = result;
                }
                catch (CompilationErrorException ex)
                {
                    if (res == null)
                    {
                        res = new FormulaEvalException(formula.Title + ex.Message);
                    }
                    continue;
                }
                catch
                {
                    continue;
                }
            }
        }

        return res;
    }

This code leads me to memory leaks and the user cannot repeat these requests. 此代码导致我内存泄漏,用户无法重复这些请求。

From my previous searches, I got some informations as my formulaContext class as to located in another project. 从以前的搜索中,我得到了一些信息,作为我的FormulaContext类与位于另一个项目中有关。 So I put it in a model project outside my API project. 因此,我将其放在API项目之外的模型项目中。 But I still have this issue. 但是我仍然有这个问题。

I tried several way to execute my script (with CSharpScript.Create or CSharpScript.EvaluateAsync for example) but the memory is still not released. 我尝试了几种执行脚本的方法(例如,使用CSharpScript.Create或CSharpScript.EvaluateAsync),但是仍然没有释放内存。

I also heard about AppDomain class to execute my script in sandbox and release memory after using but AppDomain is not used anymore in ASP.NET Core. 我还听说了AppDomain类,可以在使用后在沙箱中执行脚本并释放内存,但在ASP.NET Core中不再使用AppDomain。

Thank you for help ;) 谢谢你的帮助 ;)

Ok I found a fix : 好的,我找到了解决方法:

                finally
                {
                    GC.Collect();
                }

The memory fall from more than 1 GB to 250 MG. 内存从1 GB以上降至250 MG。 I know, once an type or assembly is loaded, it can't be unloaded anymore but the formulas are very small so I suppose my memory was full because of the CSharpScript's compilation process. 我知道,一旦加载了类型或程序集,就无法再将其卸载,但是公式很小,因此我想由于CSharpScript的编译过程,我的内存已满。

I'm waiting now for the ability to unload assemblies to definitevely clean that. 我现在正在等待卸载程序集的能力,以彻底清理该程序集。 Apparently, it is planned : https://github.com/dotnet/coreclr/issues/552 显然是有计划的: https : //github.com/dotnet/coreclr/issues/552

Thank Tseng for your help. 感谢曾先生的帮助。

Why are you guys tripping out so much. 你们为什么要跳这么多。 Unloading of Assemblies/Types is NOT supported in DotNet Core and is not going to be until 3.0. DotNet Core不支持程序集/类型的卸载,并且直到3.0才会卸载。

I am working on a CMS/VFS for DotNet Core and this is the single biggest obstacle at the moment. 我正在为DotNet Core开发CMS / VFS,这是目前最大的障碍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM