This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I think the 2nd code snippet is the correct way to invoke the transformer on the set of files. However when i run this code on the same set of files, i get a OutOfMemory Exception.
Can any one let me know why i don't get a memory exception with first snippet when a new transformer is created for every iteration?
Is there something additional i need to take care of while doing the transformation?
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
raj jitu malhotra wrote:2. as a transformer is created again and again there should be more object creation (memory allocation/de-allocation) in first snippet than the 2nd snippet.
Indeed that's correct -- as far as your code is concerned. What is happening is that the Transformer caches various things that it produces. And then when you reuse it, it caches more of those things. Eventually you run out of memory. But reducing the number of objects created is usually a pointless goal. Especially in your case, where you were trying to avoid creating a single object in a loop which would normally create hundreds or thousands of objects. Try to keep things in perspective in the future.
Anyway, last time I went through the tutorials about XSLT in Java, the recommendation was to cache a Templates object rather than a Transformer object. If you really find you have to optimize things, then that would be the way to go. But don't bother until you really find you have to optimize things.
raj jitu malhotra
Joined: Feb 05, 2012
thanks paul, that helps...
can you elaborate on your point.....
But reducing the number of objects created is usually a pointless goal.
Joined: Apr 16, 2008
You attempted to reduce the number of objects created in a for loop and broke the code with a OutOfMemory Exception. In reality, your refactoring ended up creating more objects in the JVM. Sounds pointless to me...
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com