简体   繁体   English

C# - Visual Studio中的System.OutOfMemoryException

[英]C# - System.OutOfMemoryException in Visual Studio

I have a problem, when I right click my main form in Visual Studio and go to 'View Designer' I get an error. 我有一个问题,当我在Visual Studio中右键单击我的主窗体并转到“查看设计器”时出现错误。 It says: 'Exception of type 'System.OutOfMemoryException' was thrown.' 它说:'抛出了'System.OutOfMemoryException'类型的异常。

Stacktrace: 堆栈跟踪:

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)  

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

I got this error this morning, but I still haven't resolved it. 今天早上我收到了这个错误,但我还是没有解决。 If anyone could help me I would really appreciate it! 如果有人能帮助我,我会非常感激!

I'm only using ~55% of all my RAM, so that can't be it. 我只使用了我所有RAM的55%,所以不可能。

As Dr Hebie points out, it's doubtful that it's VS itself throwing the OOM exception, but something in your form constructor. 正如Hebie博士指出的那样,令人怀疑的是VS本身是否会抛出OOM异常,而是在你的表单构造函数中。

A Technique I've used to great success with this is opening the form code and inserting Throw new Exception("Message describing position") at the start of the constructor. 我以前用过的技术就是打开表单代码并在构造函数的开头插入Throw new Exception("Message describing position") Hopefully, now instead of getting an OOM exception, you'll get the exception you just specified. 希望现在不是获得OOM异常,而是获得刚刚指定的异常。 Now move this exception around until you get the OOM exception. 现在移动此异常,直到您获得OOM异常。 This will show you the line of code that is causing the OOM. 这将显示导致OOM的代码行。

Good luck! 祝好运!

There are multiple things that this can be caused by, and the problem gets worse with older version of Visual Studio (2005 was particularly bad in my experience). 这可能是由多种因素导致的,并且旧版Visual Studio的问题变得更糟(2005年我的经历特别糟糕)。

As this is happening when you view the designer of a form, there is a chance that this is due to objects being created in your form's constructor or event handlers. 当您查看表单的设计者时会发生这种情况,这可能是由于在表单的构造函数或事件处理程序中创建了对象。 When VS loads your form into the designer it will actually compile and create an instance of the form class. 当VS将表单加载到设计器中时,它实际上将编译并创建表单类的实例。 Any objects you create within the form are likely to get created at this time as well. 您在表单中创建的任何对象也可能在此时创建。 All this happens within Visual Studio's memory allocation so if you are allocating a large amount of memory this can hinder Visual Studio's memory handling. 所有这些都发生在Visual Studio的内存分配中,因此如果要分配大量内存,这可能会妨碍Visual Studio的内存处理。

I would suggest you perform a check on the DesignMode property of the form and only load/create instances of data classes (like Views) when that property is false. 我建议您对表单的DesignMode属性执行检查,并仅在该属性为false时加载/创建数据类(如Views)的实例。 You should also be prepared to do this in event handlers throughout the form as these can be fired by the Visual Studio designer. 您还应该准备在整个表单中的事件处理程序中执行此操作,因为这些可以由Visual Studio设计器触发。

Alternatively, if you're feeling brave, you can actually debug Visual Studio with itself! 或者,如果你感觉很勇敢,你可以自己调试Visual Studio! Open your project in VS and then open another instance of VS. 在VS中打开您的项目,然后打开另一个VS实例。 In the second instance use the Debug -> Attach to Process option and attach to the first VS instance. 在第二个实例中,使用Debug - > Attach to Process选项并附加到第一个VS实例。 Now open the designer for your form and see if you can identify where the error occurs. 现在打开表单的设计器,看看是否可以识别错误发生的位置。 You may have to switch on the 'break on thrown exceptions' settings under Debug -> Exceptions in the second VS instance to ensure your debugging session sees all exceptions. 您可能必须在第二个VS实例中的Debug - > Exceptions下打开“break thrown exception”设置,以确保您的调试会话看到所有异常。

Good Luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM