• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Which is best way to go for optimal memory use?

 
Stu Higgs
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

1)HashMap Scenario:
NodeList nlHeading = ((org.w3c.dom.Document)sc.getAttribute("myDom")).getElementsByTagName("heading");

NamedNodeMap headingAtrributes = nlHeading.item(0).getAttributes();

java.util.Map contactMap = new HashMap();
String key = null;

for(int i=0; i< headingAtrributes.getLength();i++){

key = headingAtrributes.item(i).getNodeName();

contactMap.put(key,headingAtrributes.item(i).getNodeValue());

}

Set keys = contactMap.keySet();
Iterator it = keys.iterator();

while(it.hasNext()){

key = (String)it.next();

Paragraph pTest = new Paragraph("Key:"+key+" VALUE: "+contactMap.get(key));

doc.add(pTest);

}

2)Lots of Local Variables Scenario:

NodeList nlHeading = ((org.w3c.dom.Document)sc.getAttribute("myDom")).getElementsByTagName("heading");

NamedNodeMap headingAtrributes = nlHeading.item(0).getAttributes();

Node name = headingAtrributes.getNamedItem("name");
Node title = headingAtrributes.getNamedItem("title");
Node street = headingAtrributes.getNamedItem("street");
Node town = headingAtrributes.getNamedItem("town");
Node state = headingAtrributes.getNamedItem("state");
Node zip = headingAtrributes.getNamedItem("zipcode");
Node phone = headingAtrributes.getNamedItem("phone");
Node email = headingAtrributes.getNamedItem("email");

String contactName = name.getNodeValue(); etc..

Which would be more memory friendly? Thanks.
 
Bruno Boehr
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

for(int i=0; i< headingAtrributes.getLength();i++){

key = headingAtrributes.item(i).getNodeName();
// contactMap.put(key,headingAtrributes.item(i).getNodeValue());
Paragraph pTest = new Paragraph("Key:"+key+" VALUE: "+key,headingAtrributes.item(i).getNodeValue());
doc.add(pTest);
}

and get rid of the second one.

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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65124
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bruno Boehr:
This is not really a servlet question...


Indeed. Moved to the XML forum.
 
Stu Higgs
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bruno. Good food for thought.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic