涟漪效应:OutOfMemoryException

时间:2016-01-20 01:14:57

标签: c# scripting roslyn .net-4.6

我一直在努力了解Roslyn,看看它是否能满足我的需求。

在一个非常简单的项目中,我试图创建一个简单的“涟漪效应”,每次迭代都会导致加载一个新的程序集,最终在500次迭代后崩溃(OutOfMemoryException)

有没有办法做到这一点而不会让它爆炸?

class Program
{
    static void Main(string[] args)
    {
        string code = @"
        IEnumerable<double> combined = A.Concat(B);
        return combined.Average();                    
        ";

        Globals<double> globals = new Globals<double>()
        {
            A = new double[] { 1, 2, 3, 4, 5 },
            B = new double[] { 1, 2, 3, 4, 5 },
        };

        ScriptOptions options = ScriptOptions.Default;            
        Assembly systemCore = typeof(Enumerable).Assembly;
        options = options.AddReferences(systemCore);
        options = options.AddImports("System");
        options = options.AddImports("System.Collections.Generic");
        options = options.AddImports("System.Linq");

        var ra = CSharpScript.RunAsync(code, options, globals).Result;

        for (int i = 0; i < 1000; i++)
        {
            ra = ra.ContinueWithAsync(code).Result;
        }            
    }
}

public class Globals<T>
{
    public IEnumerable<T> A;
    public IEnumerable<T> B;
}

Exception Image

2 个答案:

答案 0 :(得分:0)

每次使用CSharpScript.Run或Evaluate方法时,实际上都会加载一个恰好很大的新脚本(.dll)。为了避免这种情况,您需要通过这样做来缓存正在执行的脚本:

_script = CSharpScript.Create<TR>(code, opts, typeof(Globals<T>)); // Other options may be needed here

缓存_script现在可以通过以下方式执行:

_script.RunAsync(new Globals<T> {A = a, B = b}); // The script will compile here in the first execution

如果每次都有几个脚本要加载应用程序,这是最简单的事情。但是,更好的解决方案是使用单独的AppDomain并加载隔离的脚本。这是一种方法:

创建一个脚本执行器代理作为MarshalByRefObject:

public class ScriptExecutor<TP, TR> : CrossAppDomainObject, IScriptExecutor<TP, TR>
{
    private readonly Script<TR> _script;
    private int _currentClients;

    public DateTime TimeStamp { get; }
    public int CurrentClients => _currentClients;
    public string Script => _script.Code;

    public ScriptExecutor(string script, DateTime? timestamp = null, bool eagerCompile = false)
    {
        if (string.IsNullOrWhiteSpace(script))
            throw new ArgumentNullException(nameof(script));

        var opts = ScriptOptions.Default.AddImports("System");
        _script = CSharpScript.Create<TR>(script, opts, typeof(Host<TP>)); // Other options may be needed here
        if (eagerCompile)
        {
            var diags = _script.Compile();
            Diagnostic firstError;
            if ((firstError = diags.FirstOrDefault(d => d.Severity == DiagnosticSeverity.Error)) != null)
            {
                throw new ArgumentException($"Provided script can't compile: {firstError.GetMessage()}");
            }
        }
        if (timestamp == null)
            timestamp = DateTime.UtcNow;
        TimeStamp = timestamp.Value;
    }

    public void Execute(TP parameters, RemoteCompletionSource<TR> completionSource)
    {
        Interlocked.Increment(ref _currentClients);
       _script.RunAsync(new Host<TP> {Args = parameters}).ContinueWith(t =>
       {
           if (t.IsFaulted && t.Exception != null)
           {
               completionSource.SetException(t.Exception.InnerExceptions.ToArray());
               Interlocked.Decrement(ref _currentClients);
           }
           else if (t.IsCanceled)
           {
               completionSource.SetCanceled();
               Interlocked.Decrement(ref _currentClients);
           }
           else
           {
               completionSource.SetResult(t.Result.ReturnValue);
               Interlocked.Decrement(ref _currentClients);
           }
       });
    }
}

public class Host<T>
{
    public T Args { get; set; }
}

创建代理对象以在脚本执行应用程序域和主域之间共享数据:

public class RemoteCompletionSource<T> : CrossAppDomainObject
{
    private readonly TaskCompletionSource<T> _tcs = new TaskCompletionSource<T>();

    public void SetResult(T result) { _tcs.SetResult(result); }
    public void SetException(Exception[] exception) { _tcs.SetException(exception); }
    public void SetCanceled() { _tcs.SetCanceled(); }

    public Task<T> Task => _tcs.Task;
}

创建所有其他远程需要继承的辅助抽象类型:

public abstract class CrossAppDomainObject : MarshalByRefObject, IDisposable
{

    private bool _disposed;

    /// <summary>
    /// Gets an enumeration of nested <see cref="MarshalByRefObject"/> objects.
    /// </summary>
    protected virtual IEnumerable<MarshalByRefObject> NestedMarshalByRefObjects
    {
        get { yield break; }
    }

    ~CrossAppDomainObject()
    {
        Dispose(false);
    }

    /// <summary>
    /// Disconnects the remoting channel(s) of this object and all nested objects.
    /// </summary>
    private void Disconnect()
    {
        RemotingServices.Disconnect(this);

        foreach (var tmp in NestedMarshalByRefObjects)
            RemotingServices.Disconnect(tmp);
    }

    public sealed override object InitializeLifetimeService()
    {
        //
        // Returning null designates an infinite non-expiring lease.
        // We must therefore ensure that RemotingServices.Disconnect() is called when
        // it's no longer needed otherwise there will be a memory leak.
        //
        return null;
    }

    public void Dispose()
    {
        GC.SuppressFinalize(this);
        Dispose(true);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (_disposed)
            return;

        Disconnect();
        _disposed = true;
    }
}

以下是我们如何使用它:

public static IScriptExecutor<T, R> CreateExecutor<T, R>(AppDomain appDomain, string script)
    {
        var t = typeof(ScriptExecutor<T, R>);
        var executor = (ScriptExecutor<T, R>)appDomain.CreateInstanceAndUnwrap(t.Assembly.FullName, t.FullName, false, BindingFlags.CreateInstance, null, 
            new object[] {script, null, true}, CultureInfo.CurrentCulture, null);
        return executor;
    }

public static AppDomain CreateSandbox()
{
    var setup = new AppDomainSetup { ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase };
    var appDomain = AppDomain.CreateDomain("Sandbox", null, setup, AppDomain.CurrentDomain.PermissionSet);
    return appDomain;
}

string script = @"int Square(int number) {
                      return number*number;
                  }
                  Square(Args)";

var domain = CreateSandbox();
var executor = CreateExecutor<int, int>(domain, script);

using (var src = new RemoteCompletionSource<int>())
{
    executor.Execute(5, src);
    Console.WriteLine($"{src.Task.Result}");
}

请注意在使用块中使用RemoteCompletionSource。如果您忘记丢弃它,您将有内存泄漏,因为另一个域(而不是调用者)上的此对象的实例永远不会被GC。

免责声明:我从中获取了RemoteCompletionSource的想法 here,也是来自公共领域的CrossAppDomainObject的想法。

答案 1 :(得分:0)

根据以下评论(下面再次复制);对此有疑问;我有许多动态脚本将被加载,而不是缓存我喜欢之后立即处理的脚本;那可能吗?

  

&#34;每次使用CSharpScript.Run或Evaluate方法时,实际上都在加载一个恰好很大的新脚本(.dll)。为了避免这种情况,您需要缓存正在执行的脚本&#34;