Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8498009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:17:09+00:00 2026-06-11T00:17:09+00:00

can anyone tell us what’s wrong with this code below? we have here an

  • 0

can anyone tell us what’s wrong with this code below?
we have here an object serializer that should return an XML string of any object passed to it.

we’ve been scratching our heads over this one, as we have a program that calls this multiple times and we see our memory usage going sky high (and staying there even after the program was done).. we’ve done searches but to no avail. the stream object is inside a “using” statement so we thought this was supposed to be disposed on its own.. help please.

    public static string ToXML(this IMessage m)
    {          
        try
        {
            var serializer = SerializerFactory.Create(m.GetType());
            using (var stream = new MemoryStream())
            {
                serializer.Serialize(new[] { m }, stream);
                stream.Position = 0;
                var s = Encoding.ASCII.GetString(stream.ToArray());
                return s;
            }
        }
        catch (Exception e)
        {
            return string.Format("Message unserializable: {0}", e.Message);
        }
    }

btw SerializerFactory looks like this:

public class SerializerFactory
{
    public static IMessageSerializer Create(Type t)
    {
        var types = new List<Type> { t };
        var mapper = new MessageMapper();
        mapper.Initialize(types);
        var serializer = new XmlMessageSerializer(mapper);

        serializer.Initialize(types);

        return serializer;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T00:17:11+00:00Added an answer on June 11, 2026 at 12:17 am

    There is nothing hugely wrong with that code; note that using is essentially a no-op on a MemoryStream, since it only has managed resources, and managed resources are the domain of the GC; it is normal for the GC not to worry hugely until it makes sense to collect some memory, so I wouldn’t stress too much – or: if there is a problem, it probably isn’t this.

    One observation, though, would be that you can avoid a buffer in the encode step:

    var s = Encoding.ASCII.GetString(stream.GetBuffer(), 0, (int)stream.Length);
    

    and actually, I’d be tempted to use UTF8 by default, not ASCII.

    A final thought: is your SerializerFactory itself doing something that leaks? For example, are you creating a new XmlSerializer(...) via any of the more complex constructors? The simplest form:

    new XmlSerializer(typeof(SomeType));
    

    is fine – it internally caches the internal/actual serializer per-type, and re-uses this for each XmlSerializer instance created this way. However, it does not 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 are never unloaded – so yes, that can cause a memory leak. I would be very keen to see how the serializer instances are created, to see if that is the actual problem. Note that such cases are usually very easy to fix by creating your own serializer-cache in the factory:

    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<Type> { t };
                var mapper = new MessageMapper();
                mapper.Initialize(types);
                var serializer = new XmlMessageSerializer(mapper);
    
                serializer.Initialize(types);
    
                cache[t] = serializer;
    
                return serializer;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone tell the block animation equivalent of this below code snippet? [UIView beginAnimations:@View
Can anyone tell me why this code would not be working? $('body').on('test', function() {
Can anyone tell me why this code would throw an undefined is not a
Can anyone tell me why this code crashes with SIGABRT unrecognised selector sent to
can anyone tell me why this animation isn't starting? i've tried putting code in
Can anyone tell me why this query would return this record? query: db.sales.findOne({qualified: true,
Can anyone tell me what is wrong with this? I'm getting a syntax error
Can anyone tell me the meta tags that should be included in a page
Can anyone tell me what exactly does this Java code do? SecureRandom random =
Can anyone tell me what is j and i in this .cs code? private

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.