C#.Net: Out of memory exception while creating XML string with XML Searlization

Hello,

I've a project in which I pass collection and it converts in XML string.
But when below method creates 50 to 60 MB files then it does create 2 or 3 files but after that it does day "Out of memory exception"

Note: the machine RAM is 4 GB

I am using below method:

public string ToXMLString(CollectionData data)
{
           
            StringWriter stringWriter = new StringWriter();
            XmlTextWriter writer = new XmlTextWriter(stringWriter);
           
            writer.Formatting = Formatting.Indented;
           
            XmlSerializer serializer = new XmlSerializer(typeof(CollectionData));
           
            serializer.Serialize(writer, data);
            writer.Flush();
            string XmlizedString = stringWriter.ToString();
            stringWriter.Close();
            writer.Close();
            return XmlizedString;
}

Could you please look into it on urgent basis?

Best Regards,
Mohit Pandit
LVL 5
MohitPanditAsked:
Who is Participating?
 
jonnidipConnect With a Mentor Commented:
This is a simplified version of the method I use to serialize objects.
Please give it a try:
using (MemoryStream ms = new MemoryStream())
{
    new XmlSerializer(typeof(CollectionData)).Serialize(ms, data);
    ms.Position = 0;

    using (StreamReader sr = new StreamReader(ms))
    {
        return sr.ReadToEnd();
    }
}

Open in new window


Regards.
0
 
käµfm³d 👽Commented:
Try disposing of your objects:

public string ToXMLString(CollectionData data)
{
           
            StringWriter stringWriter = new StringWriter();
            XmlTextWriter writer = new XmlTextWriter(stringWriter);
            
            writer.Formatting = Formatting.Indented;
            
            XmlSerializer serializer = new XmlSerializer(typeof(CollectionData));
            
            serializer.Serialize(writer, data);
            writer.Flush();
            string XmlizedString = stringWriter.ToString();
            stringWriter.Close();
            stringWriter.Dispose();
            writer.Close();
            writer.Dispose();
            return XmlizedString;
}

Open in new window

0
 
Arthur_WoodCommented:
alternatively:

public string ToXMLString(CollectionData data)
{
           
            using (StringWriter stringWriter = new StringWriter()){
                  using (XmlTextWriter writer = new XmlTextWriter(stringWriter)){
           
                        writer.Formatting = Formatting.Indented;
           
                        XmlSerializer serializer = new XmlSerializer(typeof(CollectionData));
           
                        serializer.Serialize(writer, data);
                        writer.Flush();
                       string XmlizedString = stringWriter.ToString();
                       return XmlizedString;
             }
      }
}

AW
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Naman GoelSoftware engineer 1Commented:
Yes, Both XmlTextWriter is having File Handle and Stream Handle so you should dispose that so that it will cleanup unmanaged resources, The best way to do this is by using "using" block as suggested by  Arthur_Wood.
0
 
Arthur_WoodCommented:
like this:

public string ToXMLString(CollectionData data)
{
           
            using (StringWriter stringWriter = new StringWriter()){
                  using (XmlTextWriter writer = new XmlTextWriter(stringWriter)){
           
                        writer.Formatting = Formatting.Indented;
           
                        using (XmlSerializer serializer = new XmlSerializer(typeof(CollectionData))){

                             serializer.Serialize(writer, data);
                        }
                        writer.Flush();
                       string XmlizedString = stringWriter.ToString();
                       return XmlizedString;
             }
      }
}
0
 
käµfm³d 👽Commented:
The best way to do this is by using "using" block as suggested by  Arthur_Wood.
I wouldn't go so far as to say that it's necessarily the "best" method to do it, but it is recommended practice to do such. (There are rare occurrences where a using actually complicates debugging.)
0
 
MohitPanditAuthor Commented:
Hi AW,

I've used your code, but it is giving an error on below line:

using (XmlSerializer serializer = new XmlSerializer(typeof(CollectionData))){

The error is "System.Xml.Serialization.XmlSerializer: type used in using statement must be implicitly convertible to 'System.IDisposable'"

Could you please look into it?


Best Regards,
Mohit Pandit
0
 
Naman GoelSoftware engineer 1Commented:
XmlSerializer is not a disposable object so it should not be in using block, you should try  solution proposed by Arthur_Wood .
0
 
MohitPanditAuthor Commented:
Ok, thanks. let me try
0
 
Arthur_WoodCommented:
public string ToXMLString(CollectionData data)
{
           
            using (StringWriter stringWriter = new StringWriter()){
                  using (XmlTextWriter writer = new XmlTextWriter(stringWriter)){
           
                        writer.Formatting = Formatting.Indented;
           
                        XmlSerializer serializer = new XmlSerializer(typeof(CollectionData))

                        serializer.Serialize(writer, data);
                        writer.Flush();
                       string XmlizedString = stringWriter.ToString();
                       return XmlizedString;
             }
      }
}
0
 
MohitPanditAuthor Commented:
Hi AW,

I've used but still it is giving same exception i.e. out of memory :-(...

do you have other thoughts?

Best Regards
0
 
Naman GoelSoftware engineer 1Commented:
are you having any memory profiler, if so please use http://memprofiler.com/ and check which object is causing the problem.
0
 
Arthur_WoodCommented:
There would appear to be a memory leak somewhere.  It is probably not in the code that you have been working with.  That is why naman's suggestion should get you looking in the right place.

AW
0
 
käµfm³d 👽Commented:
How quickly are you calling this code? Disposal is not guaranteed to be instantaneous. If you had a tight enough loop running, it would still be possible to have disposed of objects in memory whilst you are attempting to create new objects.
0
 
MohitPanditAuthor Commented:
thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.