?
Solved

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

Posted on 2012-09-12
15
Medium Priority
?
6,893 Views
Last Modified: 2012-12-21
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
0
Comment
Question by:MohitPandit
  • 4
  • 4
  • 3
  • +2
15 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38390376
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
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 38391289
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
 
LVL 13

Expert Comment

by:Naman Goel
ID: 38391317
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 38391871
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38392726
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
 
LVL 5

Author Comment

by:MohitPandit
ID: 38394205
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
 
LVL 13

Expert Comment

by:Naman Goel
ID: 38394248
XmlSerializer is not a disposable object so it should not be in using block, you should try  solution proposed by Arthur_Wood .
0
 
LVL 5

Author Comment

by:MohitPandit
ID: 38394260
Ok, thanks. let me try
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 38394321
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
 
LVL 5

Author Comment

by:MohitPandit
ID: 38394684
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
 
LVL 13

Expert Comment

by:Naman Goel
ID: 38395061
are you having any memory profiler, if so please use http://memprofiler.com/ and check which object is causing the problem.
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 38395315
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38395502
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
 
LVL 13

Accepted Solution

by:
jonnidip earned 1500 total points
ID: 38417058
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
 
LVL 5

Author Closing Comment

by:MohitPandit
ID: 38714897
thanks
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Simple Linear Regression

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question