我有一些C#代码,它读取巨大的文件,经过一些操作后,将其引用设置为null并退出函数,但内存不会释放。即使退出函数后,.NET内存也不能释放
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlString);
XmlService.ConvertExcelToXML(xmlDoc);
int sdfid = 320;
XmlService.CompareXML(xmlDoc, ref sdfid, pkid);
xmlDoc.RemoveAll();
xmlDoc = null;
xmlDoc
是一个非常大的字符串,通常大约50 MB。当我退出该功能时,该内存被永久占用,而且我必须每天重新启动我的服务几次,否则内存使用量将达到1GB。
我试图使用GC.Collect,但没有用。
预先感谢您。
编辑
这里是XmlService的类声明。它没有变数。所有的方法都是静态的
public class XmlService
ConvertExcelToXML函数的代码
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;
}
和规范CompareXML
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);
}
filexmlDoc传递给GetMaxSDFID只是被transversed由节点节点没有更新/ delation完成
发布了XmlService.ConvertExcelToXML和XmlService.CompareXML的代码 – 2011-05-01 07:39:15
我怀疑问题出在'XmlService'方法 - 它有没有可能是不安全/非托管代码?另一个选择是它使用'static'变量,从而将它保存在内存中。 – 2011-05-01 07:40:04
你确定这段代码导致了这个问题吗?如何完成Excel转换?或者在CompareXML方法中做了什么。 – ChristiaanV 2011-05-01 07:40:07