XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlString);
XmlService.ConvertExcelToXML(xmlDoc);
int sdfid = 320;
XmlService.CompareXML(xmlDoc, ref sdfid, pkid);
xmlDoc.RemoveAll();
xmlDoc = null;
xmlDoc
是一个非常大的字符串,通常约为 50MB。在退出函数时,该内存被永久占用,我不得不每天重新启动我的服务几次,否则它的内存使用量会达到 1GB。
我尝试使用 GC.Collect,但是没有效果。
提前谢谢。
编辑
这是 XmlService 的类声明。它没有变量。所有方法都是静态的。
public class XmlService
将Excel转换为XML的函数代码
public static bool ConvertExcelToXML(XmlDocument xmlDoc) {
XmlNamespaceManager nm = new XmlNamespaceManager(xmlDoc.NameTable);
nm.AddNamespace("z", "urn:schemas-microsoft-com:office:spreadsheet");
nm.AddNamespace("o", "urn:schemas-microsoft-com:office:office");
nm.AddNamespace("x", "urn:schemas-microsoft-com:office:excel");
nm.AddNamespace("ss", "urn:schemas-microsoft-com:office:spreadsheet");
nm.AddNamespace("html", "http://www.w3.org/TR/REC-html40");
XmlNodeList rows = xmlDoc.DocumentElement.SelectNodes("//z:Worksheet/z:Table/z:Row", nm);
if (rows != null && rows.Count > 0)
{
XmlNode nodeNames = rows[0];
XmlNode nodeValues = rows[1];
XmlNode destRootNode = xmlDoc.CreateNode(XmlNodeType.Element, "ParentNode", null);
XmlNode fieldNode = null;
XmlNode dataNode = null;
for (int i = 0; i < nodeNames.ChildNodes.Count; i++)
{
if (nodeNames.ChildNodes[i].HasChildNodes)
{
string nodeName = nodeNames.ChildNodes[i].ChildNodes[0].InnerXml;
//string nodeValue = nodeValues.ChildNodes[i].ChildNodes[0].InnerXml;
string nodeValue = "DataField" + i.ToString();
fieldNode = xmlDoc.CreateNode(XmlNodeType.Element, "Field", null);
dataNode = xmlDoc.CreateNode(XmlNodeType.Element, "Data", null);
dataNode.InnerXml = nodeName;
fieldNode.AppendChild(dataNode);
destRootNode.AppendChild(fieldNode);
fieldNode = xmlDoc.CreateNode(XmlNodeType.Element, "Field", null);
dataNode = xmlDoc.CreateNode(XmlNodeType.Element, "Data", null);
dataNode.InnerXml = nodeValue;
fieldNode.AppendChild(dataNode);
destRootNode.AppendChild(fieldNode);
}
}
xmlDoc.LoadXml("<ParentNode>" + destRootNode.InnerXml + "</ParentNode>");
return true;
}
return false;
}
并且比较XML的代码
public static void CompareXML(XmlDocument filexmlDoc, ref int maxSDFID, string PKID)
{
FieldsListBO tmpFieldListBO = null;
ResponseDTO responseDTO = DbService.getConnection();
DbConnection con = (DbConnection)responseDTO.ReturnedObjects[Constants.CONNECTION_OBJECT];
DbProviderFactory factory = (DbProviderFactory)responseDTO.ReturnedObjects[Constants.FACTORY_OBJECT];
DbCommand cmd = factory.CreateCommand();
cmd.CommandText = "select * from tree_store";
cmd.Connection = con;
con.Open();
DbDataReader dr = cmd.ExecuteReader();
dr.Read();
String pXmlizedString = (String)dr["TransactionTree"];
dr.Dispose();
cmd.Dispose();
con.Dispose();
XmlSerializer xs = new XmlSerializer(typeof(FieldsListBO));
MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.ASCII);
tmpFieldListBO = (FieldsListBO)xs.Deserialize(memoryStream);
memoryStream.Dispose();
xmlTextWriter.Close();
if (tmpFieldListBO.FieldsList.Count < 1)
{
maxSDFID = 0;
return;
}
FieldsListBO fieldListBO = new FieldsListBO();
for (int i = 0; i < tmpFieldListBO.FieldsList.Count; i++)
{
if (tmpFieldListBO.FieldsList[i]._pkid.Equals(PKID))
{
fieldListBO.FieldsList.Add(tmpFieldListBO.FieldsList[i]);
}
}
GetMaxSDFID(filexmlDoc, ref maxSDFID, fieldListBO);
}
传递给GetMaxSDFID的filexmlDoc仅仅是按节点遍历,没有进行更新/删除操作
XmlService
的方法上 - 有没有可能是不安全/非托管代码?另一个选项是它正在使用static
变量,从而将其保留在内存中。 - Shadow The Spring Wizard