I keep getting a 'System.OutOfMemoryException' thrown at the code below. I can't figure out where the memory leak is and it would be a great help if someone could explain what I am doing wrong. Thanks!
lock ((_tabs))
{
System.IO.StreamReader sr = null;
System.IO.MemoryStream ms = null;
try
{
Type[] t = { typeof(tsgPublicDecs.tsgClsTab) };
System.Xml.Serialization.XmlSerializer srl = new System.Xml.Serialization.XmlSerializer(typeof(ArrayList), t);
ms = new System.IO.MemoryStream();
srl.Serialize(ms, _tabs);
ms.Seek(0, 0);
sr = new System.IO.StreamReader(ms);
return sr.ReadToEnd();
}
finally
{
if (((sr != null)))
{
sr.Close();
sr.Dispose();
}
if (((ms != null)))
{
ms.Close();
ms.Dispose();
}
}
}
EDIT: To Answer a few of the questions:
- _tabs is not being populated with anything (Which brings up many other questions why its even being used but I'll need to ask the developer who wrote it for that)
- The line throwing the error is 'srl.Serialize(ms, _tabs);'
- This error is random and I've been unable to duplicate it myself but letting it run over a few days this will be thrown. Because of this I am unable (don't know how) to get any information beyond the error being thrown.
EDIT 2: Thanks for all the input. Adding usings and looking for other possible memory leaks seems like the best approach. Its great to see how quickly people can lend a hand!