out of memory exception

Topics: Data Access Application Block
Sep 27, 2011 at 10:52 PM

I am getting an out of memory exception while trying to load the connection strings from a parent source file.


The code looks like this: 

            IConfigurationSource configSource = ConfigurationSourceFactory.Create();
            ConnectionStringsSection connSection = (ConnectionStringsSection)configSource.GetSection("connectionStrings");
and the stack trace for the exception is this:
An error occurred loading a configuration file: Exception of type 'System.OutOfMemoryException' was thrown. (C:\IB\IBServices\int_shared.config)
Exception of type 'System.OutOfMemoryException' was thrown.
   at System.String.GetStringForStringBuilder(String value, Int32 startIndex, Int32 length, Int32 capacity)
   at System.Text.StringBuilder.GetNewString(String currentString, Int32 requiredLength)
   at System.Text.StringBuilder.Append(Char[] value, Int32 startIndex, Int32 charCount)
   at System.Xml.BufferBuilder.Append(Char[] value, Int32 start, Int32 count)
   at System.Xml.XmlTextReaderImpl.ParseAttributeValueSlow(Int32 curPos, Char quoteChar, NodeData attr)
   at System.Xml.XmlTextReaderImpl.ParseAttributes()
   at System.Xml.XmlTextReaderImpl.ParseElement()
   at System.Xml.XmlTextReaderImpl.ParseElementContent()
   at System.Xml.XmlTextReaderImpl.Read()
   at System.Xml.XmlTextReader.Read()
   at System.Xml.XmlTextReaderImpl.Skip()
   at System.Xml.XmlTextReader.Skip()
   at System.Configuration.XmlUtil.StrictSkipToNextElement(ExceptionAction action)
   at System.Configuration.BaseConfigurationRecord.ScanSectionsRecursive(XmlUtil xmlUtil, String parentConfigKey, Boolean inLocation, String locationSubPath, OverrideModeSetting overrideMode, Boolean skipInChildApps)
   at System.Configuration.BaseConfigurationRecord.ScanSections(XmlUtil xmlUtil)
   at System.Configuration.BaseConfigurationRecord.InitConfigFromFile()
   at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal)
   at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors)
   at System.Configuration.Configuration..ctor(String locationSubPath, Type typeConfigHost, Object[] hostInitConfigurationParams)
   at System.Configuration.ClientConfigurationHost.OpenExeConfiguration(ConfigurationFileMap fileMap, Boolean isMachine, ConfigurationUserLevel userLevel, String exePath)
   at System.Configuration.ConfigurationManager.OpenExeConfigurationImpl(ConfigurationFileMap fileMap, Boolean isMachine, ConfigurationUserLevel userLevel, String exePath)
   at System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(ExeConfigurationFileMap fileMap, ConfigurationUserLevel userLevel)
   at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource.UpdateCache(Boolean forceUpdate)
   at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource.GetConfiguration()
   at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource.DoGetSection(String sectionName)
   at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileBasedConfigurationSource.GetSection(String sectionName)
   at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.HierarchicalConfigurationSourceHandler.DoCheckGetSection(String sectionName, ConfigurationSection configurationSection)
   at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceHandler.CheckGetSection(String sectionName, ConfigurationSection configurationSection)
   at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileBasedConfigurationSource.GetSection(String sectionName)

I can not reproduce it, and generally works fine, but I want to understand why this might happen again.

Sep 28, 2011 at 3:48 AM

When you got this, what was the memory usage in the Task Manager? Did it shoot up to 99%?

Sep 28, 2011 at 3:59 AM

I don't know, the error happened on a test server, and when I tried to reproduce it there the error didn't happen again.

Sep 28, 2011 at 5:17 AM

There are many reasons as to why OutOfMemory will happen; what you described is not actually confined to Ent Lib 5 and there have been many instances that OutOfMemory happened without using Ent Lib 5...

Is the server, by any chance, opening up multiple threads? This may cause OOM as well.

In my personal experience, if a jpg file is corrupt, it causes OOM.

There are multiple causes for OOM; you'll just have to watch out for it.

Hope this helps! :-)

Sep 28, 2011 at 2:57 PM

Well, we didn't had any problems until we migrated to enterprise library 5.0, that is why I am thinking that maybe I am not calling it right or not disposing of an object properly. Before I used ConfigurationManager to get connection strings, so maybe IConfigurationSource needs special handling?

Sep 29, 2011 at 3:02 AM

Well to be honest since this is a random occurence and you cannot repro it, there's no telling how or when it might happen again; if you have access to the code, I suggest surrounding that piece of code you mentioned in the first post with a try block and in the catch block, catch OutOfMemoryException and use the Logging Application Block and write out crucial information, such as memory usage, processor usage, etc...

Oct 4, 2011 at 10:04 PM

I managed to fix this. It seems that even if the configuration source and sections were declared a method and I set them to null before leaving the method, the memory was not cleared. After declaring the section as a static member in the class and reinitializing it only if there was a change the memory leak stopped.

Oct 5, 2011 at 7:25 AM

You can log this to the issue tracker found here: http://entlib.codeplex.com/workitem/list/basic