Memory leak in XmlSerializer

3

I have the code below. Since the method is static and XmlSerializer does not implement Dispose , every method call, the system heaps in memory or GC garbage collector ) can clear this variable ?

And in the case of classes that do not have Dispose , to destroy the variable can I do converter = null , or do I have to do something else? For 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;
    }
}
    
asked by anonymous 27.10.2015 / 17:21

2 answers

3

XmlSerializer does not need dispose() because it does not allocate resources. It only allocates memory. So when you quit executing the method, the object will have no reference to it. It can be removed from memory at any time by the GC.

This does not mean that it will happen on time. It could be a long time. But the GC is smart enough to know the right time to remove it from memory. It will not cause you any trouble.

Local variables only exist during the execution of the method where they are contained. If your content is not escaped out of the method, the object pointed to by the variable is orphaned and can be deleted at any time.

This differs from the StringWriter that has unmanaged resources allocated and must have a default to tell you that it can be deleted. That's why you had to use using .

This code is correct and will not leak memory.

    
27.10.2015 / 17:40
2

There are basically 2 rules where implementing IDisposable is required:

  • The object maintains unmanaged resources;
  • The object maintains managed resources that implement IDisposable;
  • XmlSerializer does not fall under any of these rules.

    IDisposable is usually implemented in conjunction with ~ finalizer () so that if dispose () is not called, this will be done when the object is destroyed.

    link

    PS: A more efficient implementation of your TextObject Converter follows below:

        public static string ConverterObjetoEmTexto(object dados)
        {
            var sb = new StringBuilder();
            using (var textWriter = new EncodedStringWriter(sb, Encoding.UTF8))
            {
                new XmlSerializer(dados.GetType()).Serialize(textWriter, dados);
                return sb.ToString();
            }
        }
    

    You will need this class:

        public class EncodedStringWriter : StringWriter
        {
            private readonly Encoding encoding;
    
            public EncodedStringWriter(StringBuilder stringBuilder, Encoding encoding) : base(stringBuilder)
            {
                this.encoding = encoding;
            }
    
            public override Encoding Encoding
            {
                get
                {
                    return encoding;
                }
            }
        }
    
        
    27.10.2015 / 20:20