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...