mscorlib.dll 中的 c# System.OutOfMemoryException
c# System.OutOfMemoryException in mscorlib.dll
我正在使用 VS 2017。我的 vsix 项目出现 "OutOfMemory in mscorlib.dll" 异常。我在这个项目中使用 Roslyn,下面是我查找字段声明和引用的方法。
public static async Task FindFieldDeclarationAndReferencesAsync(string solutionPath, List<Violation> violations)
{
var msWorkspace = MSBuildWorkspace.Create();
var solution = await msWorkspace.OpenSolutionAsync(solutionPath);***//The solution I am passing to this workspace is 1 GB***
var documents = solution.Projects.SelectMany(x => x.Documents).ToList();
foreach (var violation in violations)//violations.Count is approximately 10
{
var classFile = documents.Where(x => x.FilePath == violation.FilePath).FirstOrDefault();
var model = await classFile.GetSemanticModelAsync();
var root = await classFile.GetSyntaxRootAsync();
var classDeclarationNode = root.DescendantNodes().OfType<ClassDeclarationSyntax>().Where(x => x.Identifier.Text == violation.ClassName).FirstOrDefault();
var fieldDeclarationNodeList = classDeclarationNode.DescendantNodes().OfType<FieldDeclarationSyntax>().Where(m => m.Modifiers.ToString().Contains("public const")).SelectMany(x => x.Declaration.Variables.Where(y => y.Identifier.Text == violation.FieldName)).FirstOrDefault();
ISymbol fieldSymbol = model.GetDeclaredSymbol(fieldDeclarationNodeList);
var fieldReferences = await SymbolFinder.FindReferencesAsync(fieldSymbol, solution);
violation.FieldDeclaration = fieldDeclarationNodeList;
violation.Replacement = Helpers.GetIdentifierReplacement(violation.FieldName);
violation.References = fieldReferences.SelectMany(item => item.Locations).ToList();
}
}
我是否收到此异常,因为在创建工作区时传递给 Roslyn 的解决方案大小为 1 GB?
异常详情:
System.OutOfMemoryException
HResult=0x8007000E
Message=Exception of type 'System.OutOfMemoryException' was thrown.
Source=mscorlib
StackTrace:
at System.Threading.ExecutionContext.Capture(StackCrawlMark& stackMark, CaptureOptions options) in f:\dd\ndp\clr\src\BCL\system\threading\executioncontext.cs:line 1281
at System.Threading.ExecutionContext.FastCapture() in f:\dd\ndp\clr\src\BCL\system\threading\executioncontext.cs:line 1190
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.GetCompletionAction(Task taskForTracing, MoveNextRunner& runnerToInitialize) in f:\dd\ndp\clr\src\BCL\system\runtime\compilerservices\AsyncMethodBuilder.cs:line 916
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.AwaitUnsafeOnCompleted[TAwaiter,TStateMachine](TAwaiter& awaiter, TStateMachine& stateMachine) in f:\dd\ndp\clr\src\BCL\system\runtime\compilerservices\AsyncMethodBuilder.cs:line 543
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.<ThrowAsync>b__6_1(Object state) in f:\dd\ndp\clr\src\BCL\system\runtime\compilerservices\AsyncMethodBuilder.cs:line 1034
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state) in f:\dd\ndp\clr\src\BCL\system\threading\threadpool.cs:line 1273
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) in f:\dd\ndp\clr\src\BCL\system\threading\executioncontext.cs:line 954
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) in f:\dd\ndp\clr\src\BCL\system\threading\executioncontext.cs:line 901
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() in f:\dd\ndp\clr\src\BCL\system\threading\threadpool.cs:line 1250
at System.Threading.ThreadPoolWorkQueue.Dispatch() in f:\dd\ndp\clr\src\BCL\system\threading\threadpool.cs:line 819
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback() in f:\dd\ndp\clr\src\BCL\system\threading\threadpool.cs:line 1161
我在 TaskManager 中检查了我的进程的句柄,下面是值。
我想摆脱这个异常。请提出解决方案。
我发现使用 Visual studio "Diagnostic Tools" window 占用更多内存的对象。在代码的开头和结尾截取内存屏幕截图并比较两者并解决问题。
当 violations
对象数量很大时,violation.FieldDeclaration
和 violation.References
占用了如此多的内存。
所以在每次迭代结束时在 finally
块中设置那些 properties to null
已经解决了我的问题。
我正在使用 VS 2017。我的 vsix 项目出现 "OutOfMemory in mscorlib.dll" 异常。我在这个项目中使用 Roslyn,下面是我查找字段声明和引用的方法。
public static async Task FindFieldDeclarationAndReferencesAsync(string solutionPath, List<Violation> violations)
{
var msWorkspace = MSBuildWorkspace.Create();
var solution = await msWorkspace.OpenSolutionAsync(solutionPath);***//The solution I am passing to this workspace is 1 GB***
var documents = solution.Projects.SelectMany(x => x.Documents).ToList();
foreach (var violation in violations)//violations.Count is approximately 10
{
var classFile = documents.Where(x => x.FilePath == violation.FilePath).FirstOrDefault();
var model = await classFile.GetSemanticModelAsync();
var root = await classFile.GetSyntaxRootAsync();
var classDeclarationNode = root.DescendantNodes().OfType<ClassDeclarationSyntax>().Where(x => x.Identifier.Text == violation.ClassName).FirstOrDefault();
var fieldDeclarationNodeList = classDeclarationNode.DescendantNodes().OfType<FieldDeclarationSyntax>().Where(m => m.Modifiers.ToString().Contains("public const")).SelectMany(x => x.Declaration.Variables.Where(y => y.Identifier.Text == violation.FieldName)).FirstOrDefault();
ISymbol fieldSymbol = model.GetDeclaredSymbol(fieldDeclarationNodeList);
var fieldReferences = await SymbolFinder.FindReferencesAsync(fieldSymbol, solution);
violation.FieldDeclaration = fieldDeclarationNodeList;
violation.Replacement = Helpers.GetIdentifierReplacement(violation.FieldName);
violation.References = fieldReferences.SelectMany(item => item.Locations).ToList();
}
}
我是否收到此异常,因为在创建工作区时传递给 Roslyn 的解决方案大小为 1 GB?
异常详情:
System.OutOfMemoryException
HResult=0x8007000E
Message=Exception of type 'System.OutOfMemoryException' was thrown.
Source=mscorlib
StackTrace:
at System.Threading.ExecutionContext.Capture(StackCrawlMark& stackMark, CaptureOptions options) in f:\dd\ndp\clr\src\BCL\system\threading\executioncontext.cs:line 1281
at System.Threading.ExecutionContext.FastCapture() in f:\dd\ndp\clr\src\BCL\system\threading\executioncontext.cs:line 1190
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.GetCompletionAction(Task taskForTracing, MoveNextRunner& runnerToInitialize) in f:\dd\ndp\clr\src\BCL\system\runtime\compilerservices\AsyncMethodBuilder.cs:line 916
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.AwaitUnsafeOnCompleted[TAwaiter,TStateMachine](TAwaiter& awaiter, TStateMachine& stateMachine) in f:\dd\ndp\clr\src\BCL\system\runtime\compilerservices\AsyncMethodBuilder.cs:line 543
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.<ThrowAsync>b__6_1(Object state) in f:\dd\ndp\clr\src\BCL\system\runtime\compilerservices\AsyncMethodBuilder.cs:line 1034
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state) in f:\dd\ndp\clr\src\BCL\system\threading\threadpool.cs:line 1273
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) in f:\dd\ndp\clr\src\BCL\system\threading\executioncontext.cs:line 954
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) in f:\dd\ndp\clr\src\BCL\system\threading\executioncontext.cs:line 901
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() in f:\dd\ndp\clr\src\BCL\system\threading\threadpool.cs:line 1250
at System.Threading.ThreadPoolWorkQueue.Dispatch() in f:\dd\ndp\clr\src\BCL\system\threading\threadpool.cs:line 819
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback() in f:\dd\ndp\clr\src\BCL\system\threading\threadpool.cs:line 1161
我在 TaskManager 中检查了我的进程的句柄,下面是值。
我想摆脱这个异常。请提出解决方案。
我发现使用 Visual studio "Diagnostic Tools" window 占用更多内存的对象。在代码的开头和结尾截取内存屏幕截图并比较两者并解决问题。
当violations
对象数量很大时,violation.FieldDeclaration
和 violation.References
占用了如此多的内存。
所以在每次迭代结束时在 finally
块中设置那些 properties to null
已经解决了我的问题。