XslCompiledTransform memory leak

The XslCompiledTransform class is an XSLT processor that supports the XSLT 1.0 syntax. It is a new implementation and includes performance gains when compared to the obsolete XslTransform class. The structure of the XslCompiledTransform class is very similar to the XslTransform class. The Load method loads and compiles the style sheet, while the Transform method executes the XSLT transform.

1. This class has 2 overloaded constructor with enableDebug parameter is set to true as default. Setting this to true enables you to debug the style sheet with the Microsoft Visual Studio Debugger. Please use the overloaded constructor with enableDebug value to false.

// Instantiate XSL Compiled Transformer
XslCompiledTransform xct = new XslCompiledTransform(false);

2. The XslCompiledTransform uses the CodeDom to compile the scripts
within the xsl file into .NET assembly and load them into the current
AppDomain. The Application memory space keeps growing up if we compile more XSL Files which have scripts. It is hard to release the resources loaded in your AppDomain, thus increases the Application Heap.

The possible solutions are: a) using extension objects
instead of msxsl:script blocks; b) recycling the AppDomain from time to time.

public class XslCompiledTransformCache
{
private static readonly object _syncRoot = new object();
private static Dictionary _transformers = new Dictionary();

public static XslCompiledTransform GetXslTransform(string xslFilePath)
{
XslCompiledTransform transformer = null;
if (false == _transformers.TryGetValue(xslFilePath, out transformer))
{
lock (_syncRoot)
{
if (false == _transformers.TryGetValue(xslFilePath, out transformer))
{
transformer = new XslCompiledTransform(false);
transformer.Load(xslFilePath);
_transformers.Add(xslFilePath, transformer);
}
}
}
return transformer;
}
}

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.