Reputation: 289
I am seeing memory leak issue with BinaryFormatter class in .Net 3.5 framework. I am using following method to deserialize an object. The size of the byte array passed to this method is 156MB. However, there is jump of 2.6GB after I call this method.
I found this article on MSDN that talks about this memory leak issue. http://blogs.msdn.com/b/psirr/archive/2009/11/13/interesting-memory-leak-in-net-3-5-binary-deserialization.aspx
Does anyone know if there is a solution to this problem? Is it resolved in .Net 4.0?
public static Message DeserializeContent(byte[] content)
{
var formatter = new BinaryFormatter();
Message message;
using (var stream = new MemoryStream(content))
{
message = (Message) formatter.Deserialize(stream);
stream.Close();
}
return message;
}
Upvotes: 4
Views: 1586
Reputation: 145
That's interesting. isn't the memory managed and even in case of a leak, shouldn't it be collected? Does it happen even if you dispose the binary formatter or force the garbage collector to run? However, even if it does collect the leaked memory, keep in mind that this isn't an absolute solution as there might not be enough memory to deserialize before the memory can be disposed...
Hope it will get fixed.
Upvotes: -1