Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ripple Effect: OutOfMemoryException

I have been trying to learn about Roslyn and see if it works for my needs.

In a very simple project I am trying to create a simple ‘Ripple Effect’, which is for each iteration causing a new assembly to be loaded and eventually after 500 iterations it crashes (OutOfMemoryException)

Is there a way to do this without causing it to explode?

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

like image 710
Aman Avatar asked Mar 31 '26 16:03

Aman


1 Answers

Everytime you use CSharpScript.Run or Evaluate method you are actually loading a new script (a .dll) which happens to be quite large. In order to avoid this you need te cache the script that you are executing by doing so:

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

Having _script cached you can now execute it by:

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

If you have a few scripts to load with your application each time, this is the easiest thing to do. However a better solution is to use a separate AppDomain and load the script isolated. Here is one way of doing it:

Create a script executor proxy as 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; }
}

Create a proxy object to share data between script execution app domain and the main domain:

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;
}

Create this helper abstract type that all the other remote ones need to inherit from:

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;
    }
}

Here is how we use it:

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}");
}

Note the usage of RemoteCompletionSource within a using block. If you forget to dispose it you will have memory leaks because instances of this object on the other domain (not the caller) will never get GCed.

Disclaimer: I took the idea of RemoteCompletionSource from here, also the idea for the CrossAppDomainObject from public domain.

like image 177
Elvis Avatar answered Apr 02 '26 05:04

Elvis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!