Last active
December 21, 2017 14:11
-
-
Save molinch/7eb0005141eda546fbfffbfe2292e4ce to your computer and use it in GitHub Desktop.
OutOfMemory with Roslyn scripting API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.CodeAnalysis; | |
using Microsoft.CodeAnalysis.CSharp.Scripting; | |
using Microsoft.CodeAnalysis.Scripting; | |
using Microsoft.CSharp.RuntimeBinder; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
using System; | |
using System.Collections.Generic; | |
using System.Collections.Immutable; | |
using System.Dynamic; | |
using System.Linq; | |
using System.Reflection; | |
using System.Runtime.CompilerServices; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Demo | |
{ | |
[TestClass] | |
public class DemoOutOfMemory | |
{ | |
[TestMethod] | |
public async Task Test() | |
{ | |
var refs = new List<MetadataReference>{ | |
MetadataReference.CreateFromFile(typeof(RuntimeBinderException).GetTypeInfo().Assembly.Location), | |
MetadataReference.CreateFromFile(typeof(DynamicAttribute).GetTypeInfo().Assembly.Location), | |
MetadataReference.CreateFromFile(this.GetType().GetTypeInfo().Assembly.Location), | |
}; | |
var options = ScriptOptions.Default.AddReferences(refs) | |
.AddImports("System.Math") | |
.AddImports(this.GetType().FullName); | |
// Act | |
for (var i = 0; i < 1000; i++) | |
{ | |
var expando = new ExpandoObject() as IDictionary<string, object>; | |
var globals = new Globals { g = new Dictionary<string, object>() }; | |
// this formatting is done to simulate a different formula | |
var script = CSharpScript.Create(string.Format("5+5 {0}", ""), options, typeof(Globals)); | |
var dlg = script.CreateDelegate(); | |
await dlg.Invoke(globals); | |
} | |
} | |
public class Globals // it needs to be public for Roslyn API scripting engine | |
{ | |
public dynamic g; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Test Name: Test | |
Test FullName: Demo.DemoOutOfMemory.Test | |
line 23 | |
Test Outcome: Failed | |
Test Duration: 0:05:38,0820234 | |
Result StackTrace: | |
at System.Runtime.InteropServices.Marshal.AllocHGlobal(IntPtr cb) | |
at System.Runtime.InteropServices.Marshal.AllocHGlobal(Int32 cb) | |
at System.Reflection.Internal.NativeHeapMemoryBlock..ctor(Int32 size) | |
at System.Reflection.Internal.StreamMemoryBlockProvider.ReadMemoryBlockNoLock(Stream stream, Boolean isFileStream, Int64 start, Int32 size) | |
at System.Reflection.PortableExecutable.PEReader..ctor(Stream peStream, PEStreamOptions options, Int32 size) | |
at System.Reflection.PortableExecutable.PEReader..ctor(Stream peStream, PEStreamOptions options) | |
at Microsoft.CodeAnalysis.ModuleMetadata.CreateFromStream(Stream peStream, PEStreamOptions options) | |
at Microsoft.CodeAnalysis.MetadataReference.CreateFromFile(String path, MetadataReferenceProperties properties, DocumentationProvider documentation) | |
at Microsoft.CodeAnalysis.Scripting.Hosting.RuntimeMetadataReferenceResolver.<>c.<.ctor>b__11_0(String path, MetadataReferenceProperties properties) | |
at Microsoft.CodeAnalysis.Scripting.Hosting.RuntimeMetadataReferenceResolver.CreateResolvedMissingReference(String fullPath) | |
at Microsoft.CodeAnalysis.Scripting.Hosting.RuntimeMetadataReferenceResolver.ResolveMissingAssembly(MetadataReference definition, AssemblyIdentity referenceIdentity) | |
at Microsoft.CodeAnalysis.CommonReferenceManager`2.ResolveAndBindMissingAssemblies(TCompilation compilation, ImmutableArray`1 explicitAssemblies, ImmutableArray`1 explicitModules, ImmutableArray`1 explicitReferences, ImmutableArray`1 explicitReferenceMap, MetadataReferenceResolver resolver, MetadataImportOptions importOptions, Boolean supersedeLowerVersions, ArrayBuilder`1 referenceBindings, Dictionary`2 assemblyReferencesBySimpleName, ImmutableArray`1& allAssemblies, ImmutableArray`1& metadataReferences, ImmutableArray`1& resolvedReferences, DiagnosticBag resolutionDiagnostics) | |
at Microsoft.CodeAnalysis.CommonReferenceManager`2.Bind(TCompilation compilation, ImmutableArray`1 explicitAssemblies, ImmutableArray`1 explicitModules, ImmutableArray`1 explicitReferences, ImmutableArray`1 explicitReferenceMap, MetadataReferenceResolver resolverOpt, MetadataImportOptions importOptions, Boolean supersedeLowerVersions, Dictionary`2 assemblyReferencesBySimpleName, ImmutableArray`1& allAssemblies, ImmutableArray`1& implicitlyResolvedReferences, ImmutableArray`1& implicitlyResolvedReferenceMap, DiagnosticBag resolutionDiagnostics, Boolean& hasCircularReference, Int32& corLibraryIndex) | |
at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.ReferenceManager.CreateAndSetSourceAssemblyFullBind(CSharpCompilation compilation) | |
at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.ReferenceManager.CreateSourceAssemblyForCompilation(CSharpCompilation compilation) | |
at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.GetBoundReferenceManager() | |
at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.get_SourceAssembly() | |
at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.get_Assembly() | |
at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.get_SourceModule() | |
at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.get_CommonSourceModule() | |
at Microsoft.CodeAnalysis.Compilation.get_SourceModule() | |
at Microsoft.CodeAnalysis.Compilation.CommonBindScriptClass() | |
at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.BindScriptClass() | |
at System.Lazy`1.CreateValue() | |
at System.Lazy`1.LazyInitValue() | |
at System.Lazy`1.get_Value() | |
at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.get_ScriptClass() | |
at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.GetEntryPointAndDiagnostics(CancellationToken cancellationToken) | |
at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.GetEntryPoint(CancellationToken cancellationToken) | |
at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.CommonGetEntryPoint(CancellationToken cancellationToken) | |
at Microsoft.CodeAnalysis.Compilation.GetEntryPoint(CancellationToken cancellationToken) | |
at Microsoft.CodeAnalysis.Scripting.ScriptBuilder.Build[T](Compilation compilation, DiagnosticBag diagnostics, Boolean emitDebugInformation, CancellationToken cancellationToken) | |
at Microsoft.CodeAnalysis.Scripting.ScriptBuilder.CreateExecutor[T](ScriptCompiler compiler, Compilation compilation, Boolean emitDebugInformation, CancellationToken cancellationToken) | |
at Microsoft.CodeAnalysis.Scripting.Script`1.GetExecutor(CancellationToken cancellationToken) | |
at Microsoft.CodeAnalysis.Scripting.Script`1.CreateDelegate(CancellationToken cancellationToken) | |
at Demo.DemoOutOfMemory.<Test>d__0.MoveNext() | |
--- End of stack trace from previous location where exception was thrown --- | |
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() | |
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) | |
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) | |
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task) | |
at System.Runtime.CompilerServices.TaskAwaiter.GetResult() | |
Result Message: | |
Test method Demo.DemoOutOfMemory.Test threw exception: | |
System.OutOfMemoryException: Insufficient memory to continue the execution of the program. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment