Link to home
Start Free TrialLog in
Avatar of MohitPandit
MohitPanditFlag for India

asked on

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
Avatar of kaufmed
kaufmed
Flag of United States of America image

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

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
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.
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;
             }
      }
}
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.)
Avatar of MohitPandit

ASKER

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
XmlSerializer is not a disposable object so it should not be in using block, you should try  solution proposed by Arthur_Wood .
Ok, thanks. let me try
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;
             }
      }
}
Hi AW,

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

do you have other thoughts?

Best Regards
are you having any memory profiler, if so please use http://memprofiler.com/ and check which object is causing the problem.
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
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.
ASKER CERTIFIED SOLUTION
Avatar of jonnidip
jonnidip
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks