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 Session Object space problem 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 "Session Object space problem" Watch "Session Object space problem" New topic
Author

Session Object space problem

Sanjay pts
Ranch Hand

Joined: Nov 07, 2000
Posts: 357
Hi

I'm working on web application.In JSP im using session object. If multiuser uses this JSP each session object is created for individual user(If i'm wrong please correct me ......).
Q.1) So i want to know how much space is allocated for session object on server.
Q.2) If i used 5 times session.putValue(object,object);
Will it affect the space allocated for session object on server.
Thanking You
Sanjay


Eat JAVA, Drink JAVA, Sleep Java
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Sanjay,
A session is for ONE user across all jsp and servlet of that particular web application. In other words, when we put session.setAttribute("loginId","Sanjay"), this "Sanjay" object is available for this paricular user in other jsps,servlets etc. Not for others!
Having said that, when we first time make a 'session.setValue(**,**), the fresh obj is loaded into heap memory of the server machine and an entry is put in a user specific session management HASH TABLE.
So when you make a for loop like this
case 1:
------
//What happens is there is ONLY ONE COPY of actual object referenced by myRef loaded into memory. You are trying to put the same object with SAME KEY 100 times. So HASHTABLE will each time find an already existing entry in it and will not add/replace the entry, finally only one entry to the user specific session management HASH TABLE
case 2:
-------
Here again only one copy of object referenced by myRef exixits in heap, BUT there are 100 entries in session management table , (which are just key,value pairs, NOT actual objs). This will not take much added memory, because this code adds extra key ,value pairs only in hashtable. Will not load a fresh session object into memory!
case 3:
-------
//Here 100 new fresh objects created and loaded into memory and there are 100 new key,value entries in master session management HASH TABLE. This is VERY BAD! Lots of wasted memory.
case 4:
-------
//same as case 3. 3 new session objects, occupying their own heap memory and 3 entries in HASHTABLE.
To visiualize what said, please see the diagram under 'session management' in this following jsp tutorial link.
http://developer.java.sun.com/developer/onlineTraining/JSPIntro/contents.html
regds
maha anna

[This message has been edited by maha anna (edited March 31, 2001).]
vaibhav punekar
Ranch Hand

Joined: Jan 20, 2001
Posts: 134
Hi,
Sanju and Maha Anna,
I think the cases put forth by maha anna are excellent.I cleared all the the ideas about the object ref,copy of it,heap etc.These are the basic things used in real time applications.Theoreticall y this concept was clear in my mind.But this example showed me the practical example.I hope java-gurus quote such examples to clear our ideas.In such case even a slightr mistake can lead to unnecessary space utilization.
My view point to this example is, it is always better to remove the value from session whenever not required.So that the memory is saved as well as session confusion can be greatly avoided.So put the value in session when required and remove immediately when not required .Am I right maha?
It was really a great example.Thanks maha.I hope sanju this will clear your doubt.


VAIBHAV <BR>SCJP
Sanjay pts
Ranch Hand

Joined: Nov 07, 2000
Posts: 357
Hi
Maha Anna
Thank you very much as this will solve my doughts.
thanx
sanju
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
vaibhav,
Yes. You are right. Try to minimize session objects. If we want to pass a an object,( either a String or other classes ) to another resource (servlet/jsp) first try to think this way.
Are we going to use this object for FOLLOWING requests from user? In other words , is this object 'request' specific? Is it ok if the object dies after this request is over? If so, use request.setAttribute(***,***) and grab from the forwarded resource with scope = 'request' or request.getAttribute(***) techniques.
Otherwise, if we need to refer rhis object later requests from user, then put it in session.
regds
maha anna
 
Don't get me started about those stupid light bulbs.
 
subject: Session Object space problem
 
Similar Threads
cannot reference num before super is called?????
Functions
Suggestion for filter saving
Passing objects via form submit
JVM Hot Spot issue (in 1.6)