OutOfMemoryError calling XmlSerializer.Deserialize() - not related to XML size!
- by Mike Atlas
This is a really crazy bug. The following is throwing an OutOfMemoryException, for XML snippits that are very short (e.g., <ABC def='123'/>) of one type, but not for others of the same size but a different type: (e.g., <ZYX qpr='baz'/>).
public static T DeserializeXmlNode<T>(XmlNode node)
{
try
{
return (T)new XmlSerializer(typeof(T))
.Deserialize(new XmlNodeReader(node));
}
catch (Exception ex)
{
throw; // just for catching a breakpoint.
}
}
I read in this MSDN article that if I were using XmlSerializer with additional parameters in the constructor, I'd end up generating un-cached serializer assemblies every it got called, causing an Assembly Leak. But I'm not using additional parameters in the constructor. It also happens on the first call, too, so the AppDomain is fresh.
Worse yet, it is only thrown in release builds, not debug builds.
What gives?