Thursday, December 06, 2007

XslCompiledTransform - Possible Memory Leak?

I just ran across an issue where the XslCompiledTransform object refused to die.  The code basically had a loop, inside of which a new XslCompiledTransform object was created and "applied" to an xml node.  This meant that for each iteration, I was reading the XSLT into memory. 

You would suspect that the object would go out of scope on the next iteration, or after the completion of the loop, or even after the method returned.  But no!  This wasn't a trivial XSLT file, either, so for every 100 iterations, my application would take on another 300MB of memory.  I was able to crash the app on my 4GB (debatable) machine in a few minutes. 

I know that doing things this way is a definite code smell, and I have learned from my mistake.  The code is now loading the transform in the constructor and using that inside the loop.  Since I'm not having to read in the XSLT all the time, the performance of the app has increased by at least an order of magnitude. 

Maybe Microsoft meant for this to happen, so I wouldn't write crappy code.  Just thought that I would share.  Has anyone else experienced this?

No comments: