Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is nothing <em>hugely</em> wrong with that code; note that <code>using</code> is <em>essentially</em> a no-op on a <code>MemoryStream</code>, since it only has <em>managed</em> resources, and managed resources are the domain of the GC; it is normal for the GC not to worry hugely until it makes <em>sense</em> to collect some memory, so I wouldn't stress too much - or: if there is a problem, it probably isn't this.</p> <p>One observation, though, would be that you can avoid a buffer in the encode step:</p> <pre><code>var s = Encoding.ASCII.GetString(stream.GetBuffer(), 0, (int)stream.Length); </code></pre> <p>and actually, I'd be tempted to use UTF8 by default, not ASCII.</p> <p>A final thought: is your <code>SerializerFactory</code> <em>itself</em> doing something that leaks? For example, are you creating a <code>new XmlSerializer(...)</code> via any of the <em>more complex</em> constructors? The simplest form:</p> <pre><code>new XmlSerializer(typeof(SomeType)); </code></pre> <p>is <em>fine</em> - it internally caches the <em>internal/actual</em> serializer per-type, and re-uses this for each <code>XmlSerializer</code> instance created this way. However, it does <em>not</em> do this caching for the more complex constructor overloads: it creates and loads a new dynamic assembly each time. And assemblies loaded in this way <strong><em>are never unloaded</em></strong> - so yes, <em>that</em> can cause a memory leak. I would be very keen to see how the serializer instances are created, to see if <em>that</em> is the actual problem. Note that such cases are usually very easy to fix by creating your own serializer-cache in the factory:</p> <pre><code>public class SerializerFactory { // hashtable has better threading semantics than dictionary, honest! private static readonly Hashtable cache = new Hashtable(); public static IMessageSerializer Create(Type t) { var found = (IMessageSerializer)cache[t]; if(found != null) return found; lock(cache) { // double-checked found = (IMessageSerializer)cache[t]; if(found != null) return found; var types = new List&lt;Type&gt; { t }; var mapper = new MessageMapper(); mapper.Initialize(types); var serializer = new XmlMessageSerializer(mapper); serializer.Initialize(types); cache[t] = serializer; return serializer; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload