3
I have the code below. How the method is static
and the XmlSerializer
does not implement the Dispose
, at each call of the method, the system stacks in memory or the GC
(Garbage Collector) can clear this variable?
And in the case of classes that do not have Dispose
, to destroy the variable I can do converter = null
, or do I have to do something else? For in my understanding, null
, only clears the value, but leaves the variable in the application pointer.
public static string ConverterObjetoEmTexto(object dados)
{
var retorno = "";
XmlSerializer converter = new XmlSerializer(dados.GetType());
using (StringWriter textWriter = new StringWriter())
{
converter.Serialize(textWriter, dados);
retorno = textWriter.ToString().Replace("encoding=\"utf-16\"", "encoding=\"utf-8\"");
return retorno;
}
}
If by chance it happens that the Xmlserializer it inside a Try / catch, it would not generate memory leak?
– Tiedt Tech
No, you left the method, no matter how, the variable will be destroyed and the object will be orphaned and can be collected at any time. The variable is in the stack (in this case). The object is in the heap. http://answall.com/q/3797/101
– Maniero