I can reproduce this Memory leak!

  • Thread starter Thread starter Qin Zhou
  • Start date Start date
Q

Qin Zhou

This is possible memory leak can be reproduced by the
following code. I am hoping someone can help me out with
simple solutions! Thanks in advanced!

using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.Text;
using System.Security.Policy;
using System.Reflection;

namespace MemoryLeak
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class MemoryLeak
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
string str=null;
MemoryLeak memLeak = new MemoryLeak();
try
{
string strXml, strXslt;
strXml = memLeak.ReadWholeFile(args[0]);
strXslt = memLeak.ReadWholeFile(args[1]);
str = memLeak.TranslateXMLContent(strXml, strXslt);
}

catch (Exception e)
{
Console.WriteLine("catch..."+e.Message);
}

finally
{
Console.WriteLine(str);
}
Console.ReadLine();
}

public MemoryLeak()
{

}

public string ReadWholeFile(string fileName)
{
StreamReader sr;
char[] buffer;
int ret;

sr = new StreamReader(fileName);
buffer = new char[sr.BaseStream.Length];
ret = sr.Read(buffer, 0, (int)sr.BaseStream.Length);
sr.Close();
return new String(buffer);
}

public string TranslateXMLContent(string strXml, string
strXslt)
{
string str = null;
XmlTextReader myXsltReader = null;
XmlTextReader myXmlReader = null;
StringBuilder myStringBuilder = null;
StringWriter myStringWriter = null;
XmlTextWriter myXmlWriter = null;
Evidence evd;

//Create the XslTransform and load the stylesheet.
XslTransform xslt = new XslTransform();
try
{
evd = Assembly.GetCallingAssembly().Evidence;
myXsltReader = new XmlTextReader(new StringReader
(strXslt));
//urlResolver.Credentials =
CredentialCache.DefaultCredentials;
xslt.Load(myXsltReader, null, evd);

//Load the XML data file.
myXmlReader = new XmlTextReader(new StringReader
(strXml));
XPathDocument doc = new XPathDocument(myXmlReader);
//Add an object to convert the book price.
myStringBuilder = new StringBuilder();
myStringWriter = new StringWriter(myStringBuilder);
myXmlWriter = new XmlTextWriter(myStringWriter);
xslt.Transform(doc, null, myXmlWriter, null);

//Transform.
str = myStringBuilder.ToString();
}

catch (Exception e)
{
Console.WriteLine(e.Message);
throw e;
}

finally
{
if (myXsltReader != null)
myXsltReader.Close();
if (myXmlReader != null)
myXmlReader.Close();
if (myStringWriter!= null)
myStringWriter.Close();
}
return str;
}
}
}


xsl file :
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:nqxsl="urn:netquote-com:xslt">
<msxsl:script language="CSharp" implements-prefix="nqxsl">
<![CDATA[
public string UpperCase(string str) {
return str.ToUpper();
}
]]>
</msxsl:script>
<xsl:output method="xml" indent="no" />
<xsl:template match="/">
<xsl:value-of select="nqxsl:UpperCase(.)"/>
</xsl:template>
</xsl:stylesheet>

sample xml file:
<?xml version="1.0" encoding="UTF-8"?>
<xmlRoot>test for memory leak</xmlRoot>

There is always one Evidence Class that has a strong handle
(using windbg.exe). I know this is cased by the C# code in
the xsl file. If you take the C# out everything works
fine.

I am wonder does anybody know how to solve this issue for
me? My program converts hunderds of this kind file
everyday and it causes big memory leak. I know the
extension obj will work fine but I don't want to go that
rounte at this poing.

Many thanks
Qin Zhou
 
Hi Qin,

I will have a try and get back to you as soon as I have any findings.

Sincerely,
Xiao Xie
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Qin,

Sorry for the late update.

I tried the program and added a loop for the XSL translation to repeat
infinitely. At first I also observed the memory usage curves to keep
climbing. However, after several minutes of execution, the memory usage
curves become stable and evetually stayed at 4,008 KB for Private Bytes
(the VM Size column in Task Manager). So it still seemed to be a caching
facility and no real leak happened. Did you see the same behavior?

I tried it on .NET Framework v1.1/Windows XP SP1.

Sincerely,
Xiao Xie
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Xiao Xie,

Thanks for your reply. I used windbg.exe and did a memory
dump. For those xslt file that uses c# code you will see
there is an Evidence Object has a "Strong Handle"
associates with it(I didn't do a loop, but I am not sure
loop will do because they might cache the c# code for
certain time). And those that don't have c# code do not
have any strong handle associates with it. Does this make
sense? If you want, please feel free to contact me
directly at my email address and I can send you my phone#.

Again

Thanks very much

Qin Zhou
 
Hi Qin,

Oh yes, I believe I can repro it now. I will get back to you with my
findings.

Thanks.

Sincerely,
Xiao Xie
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top