简体   繁体   中英

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. It says: 'Exception of type 'System.OutOfMemoryException' was thrown.'

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

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.

As Dr Hebie points out, it's doubtful that it's VS itself throwing the OOM exception, but something in your form constructor.

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. Hopefully, now instead of getting an OOM exception, you'll get the exception you just specified. Now move this exception around until you get the OOM exception. This will show you the line of code that is causing the 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).

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. 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.

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. You should also be prepared to do this in event handlers throughout the form as these can be fired by the Visual Studio designer.

Alternatively, if you're feeling brave, you can actually debug Visual Studio with itself! Open your project in VS and then open another instance of VS. In the second instance use the Debug -> Attach to Process option and attach to the first VS instance. 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.

Good Luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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