hi, In a Servlet, for best practice when developing components for high traffic websites, which is the best way to go for optimal performance related to memory usage? In the code example below would it be better to create many local reference variables for the DOM nodes, or to use the HashMap?
This is not really a servlet question, but anyway.
Speaking of the two scenarios: it is not the reference variables that take up memory, it's the objects themselves. Therefore, from this perspective both approaches will yield comparable results in terms of memory footprint. Now, performance-wise the second one will save you the cost of multiple in-map lookups, so in general it should be faster. But then there is another dimension to it: verbosity of code and maintainability. If all you want to do is convert data from one flat structure (as your XML contact descriptor appears to be) into another flat structure (say, request attributes or any other kind of map), then you will be better off simply iterating over whatever attributes your structure is made up of. This way if your structure changes, there will be fewer place in the code to modify.
One more thing: if you look at the role of the intermadiate HashMap, contactMap, you wil notice that it does not really do anything useful other than serving as a temporary storage for the contact attributes. You could just as well create your Paragraph objects inside the first loop:
Anyway, tasks like these (local and limited in scope) are not really much of a trouble-maker as far as memory consumption (or performance, for that matter) is concerned. So better pay attention to things that have a higher potential of causing performance issues, like database stuff, I/O, etc.
<a href="http://webjavenue.com/" target="_blank" rel="nofollow">Your first website in Java: easier than you think</a>