MohitPandit
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(Colle ctionData) );
serializer.Serialize(write r, 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
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
{
StringWriter stringWriter = new StringWriter();
XmlTextWriter writer = new XmlTextWriter(stringWriter
writer.Formatting = Formatting.Indented;
XmlSerializer serializer = new XmlSerializer(typeof(Colle
serializer.Serialize(write
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
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(Colle ctionData) );
serializer.Serialize(write r, data);
writer.Flush();
string XmlizedString = stringWriter.ToString();
return XmlizedString;
}
}
}
AW
public string ToXMLString(CollectionData
{
using (StringWriter stringWriter = new StringWriter()){
using (XmlTextWriter writer = new XmlTextWriter(stringWriter
writer.Formatting = Formatting.Indented;
XmlSerializer serializer = new XmlSerializer(typeof(Colle
serializer.Serialize(write
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(Colle ctionData) )){
serializer.Serialize(write r, data);
}
writer.Flush();
string XmlizedString = stringWriter.ToString();
return XmlizedString;
}
}
}
public string ToXMLString(CollectionData
{
using (StringWriter stringWriter = new StringWriter()){
using (XmlTextWriter writer = new XmlTextWriter(stringWriter
writer.Formatting = Formatting.Indented;
using (XmlSerializer serializer = new XmlSerializer(typeof(Colle
serializer.Serialize(write
}
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.)
ASKER
Hi AW,
I've used your code, but it is giving an error on below line:
using (XmlSerializer serializer = new XmlSerializer(typeof(Colle ctionData) )){
The error is "System.Xml.Serialization. XmlSeriali zer: type used in using statement must be implicitly convertible to 'System.IDisposable'"
Could you please look into it?
Best Regards,
Mohit Pandit
I've used your code, but it is giving an error on below line:
using (XmlSerializer serializer = new XmlSerializer(typeof(Colle
The error is "System.Xml.Serialization.
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 .
ASKER
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(Colle ctionData) )
serializer.Serialize(write r, data);
writer.Flush();
string XmlizedString = stringWriter.ToString();
return XmlizedString;
}
}
}
{
using (StringWriter stringWriter = new StringWriter()){
using (XmlTextWriter writer = new XmlTextWriter(stringWriter
writer.Formatting = Formatting.Indented;
XmlSerializer serializer = new XmlSerializer(typeof(Colle
serializer.Serialize(write
writer.Flush();
string XmlizedString = stringWriter.ToString();
return XmlizedString;
}
}
}
ASKER
Hi AW,
I've used but still it is giving same exception i.e. out of memory :-(...
do you have other thoughts?
Best Regards
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks
Open in new window