When I deserialize a dictionary with the method, I have I memory leak. Here the following test to reproduce the problem.
public static Dictionary<TKey, TValue> DeserializeDictionary<TKey, TValue>(this string iSerialization)
{
Dictionary<TKey, TValue> dic;
using (var textWriter = new StringReader(iSerialization))
{
XmlSerializer serializer = new XmlSerializer(typeof(Item<TKey, TValue>[]), new XmlRootAttribute() { ElementName = "items" });
dic = ((Item<TKey, TValue>[])serializer.Deserialize(textWriter)).ToDictionary(i => i.Key, i => i.Value);
textWriter.Close();
}
return dic;
}
public class Item<TKey, TValue>
{
[XmlAttribute]
public TKey Key;
[XmlAttribute]
public TValue Value;
}
the test :
[TestMethod]
public void test()
{
string test = "<?xml version=\"1.0\" encoding=\"utf-16\"?><items xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><ItemOfStringString Key=\"mykey\" Value=\"myvalue\" /></items>";
while(true)
{
Dictionary<string, string> tesfezf = test.DeserializeDictionary<string, string>();
}
}
Do you know where is the problem ?
EDIT: I use this method in a workerazure role, in a loop (arround 20000), and this fill the memory and throw outofmemory exception.