C# - System.OutOfMemoryException 在 Visual Studio

C# - System.OutOfMemoryException in Visual Studio

我遇到了一个问题,当我在 Visual Studio 中右键单击我的主窗体并转到 'View Designer' 时出现错误。它说:“抛出了 'System.OutOfMemoryException' 类型的异常。”

堆栈跟踪:

at System.Reflection.AssemblyName.nGetFileInformation(String s)
at System.Reflection.AssemblyName.GetAssemblyName(String assemblyFile)
at Microsoft.VisualStudio.Design.VSTypeResolutionService.AssemblyEntry.get_AssemblyName()
at Microsoft.VisualStudio.Design.VSTypeResolutionService.AssemblyEntry.get_FullName()
at Microsoft.VisualStudio.Design.VSTypeResolutionService.AssemblyEntry.get_AssemblySpec()
at Microsoft.VisualStudio.Design.VSTypeResolutionService.SearchProjectEntries(String fullName, Boolean correctThread)
at Microsoft.VisualStudio.Design.VSTypeResolutionService.System.ComponentModel.Design.IDesignTimeAssemblyLoader.GetTargetAssemblyPath(AssemblyName runtimeOrTargetAssemblyName, String suggestedAssemblyPath, FrameworkName targetFramework)
at Microsoft.VisualStudio.Shell.Design.VsTargetFrameworkUniverse.ResolveAssembly(AssemblyName assemblyName, Assembly runtimeAssembly)
at Microsoft.VisualStudio.Shell.Design.VsTargetFrameworkUniverse.GetTypeFromTargetLocation(Type type, Boolean validateBase)
at Microsoft.VisualStudio.Shell.Design.VsTargetFrameworkUniverse.GetType(Type type)
at Microsoft.VisualStudio.Shell.Design.VsTargetFrameworkUtil.GetCustomAttributes(Type type, Type filter, Boolean inherit, CustomAttributesCache cache)
at Microsoft.VisualStudio.Shell.Design.VsTargetFrameworkType.GetCustomAttributes(Type filter, Boolean inherit)
at Microsoft.VisualStudio.Shell.Design.VsTargetFrameworkAttributeCollection.GetAttributes(Type type, Type filter)
at Microsoft.VisualStudio.Shell.Design.VsTargetFrameworkAttributeCollection.GetAttributes(MemberInfo member, Type filter)
at Microsoft.VisualStudio.Shell.Design.VsTargetFrameworkAttributeCollection.get_Attributes()
at System.ComponentModel.AttributeCollection.get_Count()
at Microsoft.VisualStudio.Design.VSDesignSurface.EnsureExtensions(IComponent component)
at Microsoft.VisualStudio.Design.VSDesignSurface.CreateInstance(Type type)
at System.ComponentModel.Design.DesignerHost.System.ComponentModel.Design.IDesignerHost.CreateComponent(Type componentType, String name)
at System.ComponentModel.Design.Serialization.DesignerSerializationManager.CreateInstance(Type type, ICollection arguments, String name, Boolean addToContainer)
at System.ComponentModel.Design.Serialization.DesignerSerializationManager.System.ComponentModel.Design.Serialization.IDesignerSerializationManager.CreateInstance(Type type, ICollection arguments, String name, Boolean addToContainer)
at System.ComponentModel.Design.Serialization.TypeCodeDomSerializer.Deserialize(IDesignerSerializationManager manager, CodeTypeDeclaration declaration)
at System.ComponentModel.Design.Serialization.CodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager manager)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.VSCodeDomDesignerLoader.PerformLoad(IDesignerSerializationManager serializationManager)
at System.ComponentModel.Design.Serialization.BasicDesignerLoader.BeginLoad(IDesignerLoaderHost host)  

设计师: http://pastebin.com/hdRB5DAj

我今天早上收到这个错误,但我仍然没有解决。如果有人能帮助我,我将不胜感激!

我只使用了我所有 RAM 的 ~55%,所以不可能。

这可能是由多种原因引起的,旧版本的 Visual Studio 问题变得更糟(根据我的经验,2005 年尤其糟糕)。

当您查看窗体的设计器时会发生这种情况,这可能是由于在窗体的构造函数或事件处理程序中创建了对象。当 VS 将您的表单加载到设计器中时,它实际上会编译并创建表单实例 class。您在表单中创建的任何对象也可能在此时创建。所有这些都发生在 Visual Studio 的内存分配中,因此如果您分配大量内存,这可能会阻碍 Visual Studio 的内存处理。

我建议您对表单的 DesignMode 属性 进行检查,并且只有 load/create 个数据 class 实例(如视图),当 属性 是错误的。您还应该准备好在整个表单的事件处理程序中执行此操作,因为这些可以由 Visual Studio 设计器触发。

或者,如果您有勇气,您可以实际调试 Visual Studio 自身!在 VS 中打开您的项目,然后打开另一个 VS 实例。在第二个实例中,使用 Debug -> Attach to Process 选项并附加到第一个 VS 实例。现在打开窗体的设计器,看看是否可以确定错误发生的位置。您可能必须在第二个 VS 实例中的“调试”->“异常”下打开 'break on thrown exceptions' 设置,以确保您的调试会话看到所有异常。

祝你好运。

正如 Hebie 博士指出的那样,它是 VS 本身抛出 OOM 异常值得怀疑,但在您的表单构造函数中。

我在这方面取得巨大成功的一项技术是打开表单代码并在构造函数的开头插入 Throw new Exception("Message describing position")。希望现在您不会得到 OOM 异常,而是得到您刚刚指定的异常。现在移动这个异常,直到你得到 OOM 异常。这将向您显示导致 OOM 的代码行。

祝你好运!