环境C#4.5 E5 16G内存
代码:
try
{
CommonHelper.CGCollect();
string xml = XmlHelper.XmlSerialize(listKeyWords, Encoding.Default);
Mutex m = new Mutex();
if (!string.IsNullOrEmpty(xml))
{
DocHelper.writeXC(xml, string.Format("{0}\{1}", ConfigurationInfo._KEYWORDSPATH, pathName), FileMode.OpenOrCreate, m);
}
}
public static string XmlSerialize(object o, Encoding encoding)
{
using (MemoryStream stream = new MemoryStream())
{
XmlSerializeInternal(stream, o, encoding);
stream.Position = 0;
using (StreamReader reader = new StreamReader(stream, encoding))
{
return reader.ReadToEnd();
}
}
}
错误信息:
System.InvalidOperationException: 生成 XML 文档时出错。 ---> System.OutOfMemoryException: 引发类型为“System.OutOfMemoryException”的异常。
在 System.IO.MemoryStream.set_Capacity(Int32 value)
在 System.IO.MemoryStream.EnsureCapacity(Int32 value)
在 System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count)
在 System.Xml.XmlEncodedRawTextWriter.EncodeChars(Int32 startOffset, Int32 endOffset, Boolean writeAllToStream)
在 System.Xml.XmlEncodedRawTextWriter.FlushBuffer()
在 System.Xml.XmlEncodedRawTextWriter.RawText(Char* pSrcBegin, Char* pSrcEnd)
在 System.Xml.XmlEncodedRawTextWriter.RawText(String s)
在 System.Xml.XmlEncodedRawTextWriter.WriteStartAttribute(String prefix, String localName, String ns)
在 System.Xml.XmlEncodedRawTextWriterIndent.WriteStartAttribute(String prefix, String localName, String ns)
在 System.Xml.XmlWellFormedWriter.WriteStartAttribute(String prefix, String localName, String namespaceName)
在 System.Xml.Serialization.XmlSerializationWriter.WriteAttribute(String localName, String ns, String value)
在 Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterList1.Write7_WordsareadataModelS(String n, String ns, WordsareadataModelS o, Boolean isNullable, Boolean needType)
在 Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterList1.Write8_WordsareadataModel(String n, String ns, WordsareadataModel o, Boolean isNullable, Boolean needType)
在 Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterList1.Write9_KeyWords(String n, String ns, KeyWords o, Boolean isNullable, Boolean needType)
在 Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterList1.Write10_ArrayOfKeyWords(Object o)
--- 内部异常堆栈跟踪的结尾 ---
在 System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
在 System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o)
在 Lanwatcher.Tools.XmlHelper.XmlSerializeInternal(Stream stream, Object o, Encoding encoding) 位置
预计序列化后数据大小1G左右
这个问题如何解决呢?