This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "HashMap" Watch "HashMap" New topic
Author

HashMap

Ritu varada
Ranch Hand

Joined: Sep 08, 2004
Posts: 117
In my application, I create and save a HashMap in session in first servlet and use it in the next servlet. First of all, is it a good idea to store a hashmap in session? Secondly, when I use back button in my application to go back and forth, would creating hashmaps everytime when I go back to the first servlet pose a memory constraint ? Third of all, how do I remove a hashmap ? HashMap something = null; gave me a NULL pointer exception. Appreciate somebody's help.


SCJP,SCWCD(1.4)
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
In my application, I create and save a HashMap in session in first servlet and use it in the next servlet. First of all, is it a good idea to store a hashmap in session?

Maintaining a HashMap in the session context to persist data between requests is a common thing to do. You'll need to determine whether this approach is wise in your application given the size of the HashMap. If it's particularly large, remember that you may get one per user which may cause memory problems on your server at busy times. Only you know how much memory you have available, the maximum number of concurrent users you expect to support and the typical size of your HashMap.
Secondly, when I use back button in my application to go back and forth, would creating hashmaps everytime when I go back to the first servlet pose a memory constraint ?

If you're holding the HashMap in the session you only need to create it once. After that you just maintain it. If you don't use the session context but use request scope instead then any Hashmaps you create will be eligible for garbage collection once the response is generated. So no cumulative memory problems, no. Obviously you need to consider the time taken to create a new Hashmap each time versus maintaining a single Map in the session. If the Hashmap is small the difference is likely to be imperceptible.
Third of all, how do I remove a hashmap ? HashMap something = null; gave me a NULL pointer exception. Appreciate somebody's help.

Setting "something" to null makes the HashMap that "something" previously referred to eligible for GC. However, if you later try to access properties or methods on "something" in your code you'll get a NullPointerException. I mean, why would you remove the HashMap if you're then going to try and use it? To remove an attribute from the session context use (say) request.getSession().removeAttribute("myHashMap") [Note: this is the same as setAttribute("myHashMap", null)]. In request scope just leave it to the garbage collector. It'll clean up your objects once they're out of scope.

Hope that helps.

Jules
Ritu varada
Ranch Hand

Joined: Sep 08, 2004
Posts: 117
Thanks, Julian ! That was quite an explanation and it did clarify a lot of my concerns. But, my concern about creating new Hashmaps still remain. I will try to explain why I seem to be creating hashmaps everytime. This is an application which has its javascript embedded in out.println statements. As I said before, the user can go back and click next. When the user clicks next, the javascript code(goNext() method) executes which has some of its option values being set by java code. In this java block of code, I need to capture some details. So, I put it the hashmap and at the end of the loop, I set this hashmap in the session. So, I go the next servlet wherein, I retrieve this hashmap from the session. So, if the user goes back and forth , say 4 times, I would be creating hashmaps everytime( although the hashmap can have max of around 6-7 values). So, not sure, where and how I can remove the hashmaps once I retrieve the value from the session. I know its a lengthy explanation. I hope I made some sense. Appreciate your help. Thanks.
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
I get it. You create a new HashMap on each new request and set it in a session attribute named, say, "next_javascript". You're worried that if you create half a dozen of these HashMaps then they'll all sit in the session clogging it up and use up all your memory.

You don't need to worry. If you call session.setAttribute() with the same attribute name the new object replaces the old object and the old object becomes eligible for GC (as it no longer has a reference). The GC will do the work for you.

Have I understood your concern correctly?

Jules
Ritu varada
Ranch Hand

Joined: Sep 08, 2004
Posts: 117
That's exactly what I mean !! Thanks so much! You have been a great help!
Ritu varada
Ranch Hand

Joined: Sep 08, 2004
Posts: 117
There is one nagging little detail that I think you can clarify. in the application, they are doing a session.removeAttribute(attName) before they do a session.setAttribute. Is this a way of making the object garbage collectable explicitly ? Or would it just be forcing to create a new object everytime instead of just replacing it ? Is that a good idea ?
Appreciate your help.
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
As I said in a previous post, the following two lines have the same effect:

Consuequently the following three code snippets behave identically:



In each case removing an attribute when you're about to replace it is simply a waste of time. The "something" object becomes available for GC as soon as the second call is made (provided that there are no other references to it). Note that I say "becomes available for GC", not is GCed. You can't force the garbage collector to run ever. You can suggest that it might like to run (e.g. System.gc() - rarely necessary) but ultimately it makes its own mind up.

All this stuff's in the API docs by the way. You should have a read some time.

Jules
[ September 11, 2004: Message edited by: Julian Kennedy ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

SECOND NOTICE

"Vijaya"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Ritu varada
Ranch Hand

Joined: Sep 08, 2004
Posts: 117
Thanks, Julian. I did know that you cannot force GC, That's why the idea of removing an attribute didn't make much sense to me. But, I had to confirm it anyway.Thanks a lot for your help.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Thanks Vijaya for changing your name. It looks good.

Mark
 
 
subject: HashMap
 
Similar Threads
user session questions
Logging out from muliple applications
Who was right about using request filter to validate session?
remove all user Sessions
Invoking a new Session