aspose file tools*
The moose likes XML and Related Technologies and the fly likes Which is best way to go for optimal memory use? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Which is best way to go for optimal memory use?" Watch "Which is best way to go for optimal memory use?" New topic
Author

Which is best way to go for optimal memory use?

Stu Higgs
Ranch Hand

Joined: Jan 01, 2006
Posts: 74
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

Joined: Feb 15, 2006
Posts: 17
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.


<a href="http://webjavenue.com/" target="_blank" rel="nofollow">Your first website in Java: easier than you think</a>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

Originally posted by Bruno Boehr:
This is not really a servlet question...


Indeed. Moved to the XML forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Stu Higgs
Ranch Hand

Joined: Jan 01, 2006
Posts: 74
Thanks Bruno. Good food for thought.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Which is best way to go for optimal memory use?