My Development Blog

My Development experiences

Posts Tagged ‘Xml’

Memory Leak within XmlSerializer

Posted by ipwright83 on May 3, 2012

Within the product I work on we used XML based serialization for some of our classes, the original aim was to provide a way of storing settings that was human readable to make debugging and upgrades easier in the future, while avoid the deprecated SOAP serialization. Unfortunately XML serialization brings about it’s own problems, most recently of which we discovered was leaking memory.

I discovered the memory ramping via Process Explorer, if you don’t have this tool I recommend downloading it for free from the SysInternals suite.

This lead down a road to our XML serialization helper class. By commenting out parts of the code eventually the problem was isolated to a section of code along the following line:

private XmlSerializer GetSerializer(Type type, XmlRootAttribute root)
   return new XmlSerializer(type, root);
}

When you construct a new XmlSerializer it will generate a temporary assembly on the disk (not however if you generate the xml serialization assemblies when you build via SGen). Normally these assemblies are loaded up into the AppDomain and then cached, however not in the case of this specific constructor call. It never caches the assembly, instead it’s re-created every time and therefore leaks memory.

The solution is fairly simple, create your own cache, in our case we made the cache based upon the Type name, and the name of the XmlRootAttribute:

private readonly Dictionary cache = new Dictionary();

private XmlSerializer GetSerializer(Type type, XmlRootAttribute root)
{
    String key = type.AssemblyQualifiedName + "|" + root.ElementName;
    XmlSerializer result;

    if (cache.TryGetValue(key, out result))
        return result;

    result =  new XmlSerializer(type, root);
    cache.Add(key, result);
    return result;
}

Note that the code above isn’t threadsafe, you’d need some form of locking if you were to call this over multiple threads. This however solved our problem and gave us a nice flat memory profiler for de serialization.

Some useful links I discovered on my way:

Microsoft’s Tess Ferrandez – .NET Memory Leak: XmlSerializing your way to a Memory Leak
Microsoft Support – Memory usage is high when you create several XmlSerializer objects in ASP.NET
Microsoft Connect – XmlSerializer memory leak

A little later I’ll be discussing Binary serialization, why I’d like to move to it instead of XML serialization and how we can work around the issue of human readability.

Posted in Uncategorized | Tagged: , , , | Leave a Comment »