Profile Picture

Memory leaks in XML Serialization

Posted By Alexey Ochkov 11 Years Ago
Author
Message
Alexey Ochkov
explanationmark Posted 11 Years Ago
View Quick Profile
Forum Newbie

Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: Forum Members
Last Active: 9 Years Ago
Posts: 6, Visits: 15

I found memory leak in method:

// Nevron.Serialization.NSerializer
public object LoadFromStream(Type rootType, Stream stream, PersistencyFormat format, INSerializationFilter filter)
{

........

case PersistencyFormat.XML:
{
XmlSerializer xmlSerializer = new XmlSerializer(rootType, this.l11I1ll, this.l11IlII, null, null);
StreamReader textReader = new StreamReader(stream);
result = xmlSerializer.Deserialize(textReader);
break;
}

..............

}

(code decompiled using ILSpy)

Every time you create new instance of XmlSerializer , and every time XML serialization infrastructure dynamically generates new assemblies to serialize and deserialize specified types.

MSDN says:
To increase performance, the XML serialization infrastructure dynamically generates assemblies to serialize and deserialize specified types. The infrastructure finds and reuses those assemblies. This behavior occurs only when using the following constructors:
XmlSerializer. XmlSerializer(Type)
XmlSerializer. XmlSerializer(Type, String)
If you use any of the other constructors, multiple versions of the same assembly are generated and never unloaded, which results in a memory leak and poor performance. The easiest solution is to use one of the previously mentioned two constructors. Otherwise, you must cache the assemblies in a Hashtable, as shown in the following example.

You use another constructor to create XmlSerializer, and in this case you must reuse instance from hashtable.

This is possible to fix that or workaround?






Similar Topics


Reading This Topic