This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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
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).]
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.
Joined: Nov 07, 2000
Hi Maha Anna Thank you very much as this will solve my doughts. thanx sanju
Joined: Jan 31, 2000
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