簡體   English   中英

mscorlib.dll中的C#System.OutOfMemoryException

[英]c# System.OutOfMemoryException in mscorlib.dll

我正在使用VS2017。我的vsix項目中出現“ mscorlib.dll中的OutOfMemory”異常。 我在此項目中使用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();
    }               
}

我是否遇到此異常,因為解決方案大小為1 GB,在創建工作區時傳遞給Roslyn?

異常詳細信息:

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中的Handles中檢查了我的進程,以下是這些值。

任務管理器,手柄

我想擺脫這種例外。 請提出解決方案。

我發現使用Visual Studio“診斷工具”窗口占用更多內存的對象。 在代碼的開頭和結尾處截取了內存屏幕截圖,並比較了兩者並解決了該問題。

violations對象數量很大時, violation.FieldDeclarationviolation.References占用了太多內存。 因此,在每次迭代結束時在finally塊中將這些properties to null設置properties to null可以解決此問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM