none
Memory leak fixed now in XslCompiledTransform? RRS feed

  • Question

  • I am in the process of upgrading a project from 1.1 to 2.0/3.0 and recall an issue we had with caching XslTransform objects back during the 1.1 implementation.

    History:
    Basically the behaviour observed was that every time we ran another XML doc through our cached XslTransform object, the object would grow slightly in size.  This resulted in an eventual crash of the ASP.NET application pool.   The problem was discovered after some profiling to exist in an IDictionary or IList that contained all of the namespaces ever run through the transform.  They were the same in every XML document that was passed in, but they the object still seemed to grow.

    Our resolution was to cache not the XslTransform object but rather cache the contents of the XSLT file on the disk and simply create a new XslTransform object every time. 

    Question:
    From what I am reading the new XslCompiledTransform objects have a heavy load time, and so I should move to caching the actual loaded transform objects.  My obvious question is: has the memory leak been resolved, and thus can I run thousands of XML documents through a cahced XslCompiledTransform object wihtout it growing exponentially in size.

    Thanks!
    Tuesday, April 22, 2008 12:51 PM

Answers

  • As far as I recollect correctly the problem was with stylesheets containing scripts. If a stylesheet contains a script an assembly is created and loaded into memory. Then the assembly can not be unloaded and the memory leak occurs. Here is a KB article about this problem http://support.microsoft.com/kb/316775. I observed that the same was happening for XslCompiledTransform. According to the KB article this is a .NET Framework design limitation.

     

    The difference between XslTransform and XslCompiledTransform is that XslTransform interpreted the stylesheet each time the transformation was run. So, there is not a big difference between loading a stylesheet once and reusing the XslTransform object and loading the stylesheet each time when you want to perform a transformation. The story with XslCompiledTransform is however completely different. XslCompiledTransform compiles the Xslt to IL when stylesheet is loaded. That is why loading a stylesheet is a pretty big hit. After the stylesheet has been loaded the transformation is pretty fast. With XslCompiledTransform it is recommended to load once and reuse.

     

    You can find a few pretty interesting articles about transformation performance (including comparisons) here http://blogs.msdn.com/antosha/

     

    Pawel

    Tuesday, April 22, 2008 1:55 PM
    Moderator

All replies

  • As far as I recollect correctly the problem was with stylesheets containing scripts. If a stylesheet contains a script an assembly is created and loaded into memory. Then the assembly can not be unloaded and the memory leak occurs. Here is a KB article about this problem http://support.microsoft.com/kb/316775. I observed that the same was happening for XslCompiledTransform. According to the KB article this is a .NET Framework design limitation.

     

    The difference between XslTransform and XslCompiledTransform is that XslTransform interpreted the stylesheet each time the transformation was run. So, there is not a big difference between loading a stylesheet once and reusing the XslTransform object and loading the stylesheet each time when you want to perform a transformation. The story with XslCompiledTransform is however completely different. XslCompiledTransform compiles the Xslt to IL when stylesheet is loaded. That is why loading a stylesheet is a pretty big hit. After the stylesheet has been loaded the transformation is pretty fast. With XslCompiledTransform it is recommended to load once and reuse.

     

    You can find a few pretty interesting articles about transformation performance (including comparisons) here http://blogs.msdn.com/antosha/

     

    Pawel

    Tuesday, April 22, 2008 1:55 PM
    Moderator
  • I too have memory issues with the XslCompiledTransform even though I do not use script in my stylesheet.

    The size of the workerprocess keeps growing.
    My output shows these messages every time my xsl is loaded:
    'aspnet_wp.exe' (Managed): Loaded 'System.Xml.Xsl.CompiledQuery.15'
    'aspnet_wp.exe' (Managed): Loaded 'System.Xml.Xsl.CompiledQuery'

    using the obsolete XslTransform does not give these problems, but i'd rather use the XslCompiledTransform.

    Are there other reasons (besides script) that may cause problems?


    Aernout


    Friday, August 1, 2008 2:11 PM
  • I found the answer here:
    http://symphyl.blogspot.com/2007/04/memory-leak-using-xslcompiledtransform.html

    It appears that memory will also be consumed if debugging is enabled:
    XslCompiledTransform tr = new XslCompiledTransform(true);


    Aernout
    Monday, August 4, 2008 10:26 AM