aspose file tools*
The moose likes Servlets and the fly likes Basic question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Basic question" Watch "Basic question" New topic
Author

Basic question

Kavita Saluja
Greenhorn

Joined: Jun 05, 2001
Posts: 19
I have one basic question regarding HttpSession. If I have an object A of the size of 1 MB, and I store the same object twice in session ,like session.setAttribute("one",A); and session.setAttribute("two",B); then what would be the size of session 1 MB or 2 MB.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
A session holds only a reference to the object, it does not try to clone the object.
If the object is serializable, and the servlet engine is set up to write out sessions to disk to save space, there will be a 1MB object written out..
Bill
Kavita Saluja
Greenhorn

Joined: Jun 05, 2001
Posts: 19
Thanks for the answer !!
If that is the case, they what is the talk abt keeping sessions lightweight, i mean if the session is always only going to hold a reference for the object, then whether i store 2 refrences or 10 refrences, it should not be a big difference ? I have heard people compalining about performance because session is too heavy ...what does that actually mean then ?
Regards
Kevin Jones
Author
Ranch Hand

Joined: Oct 29, 2003
Posts: 39
I could think of several reasons.
i) If the session is serialized then you need to serialize all the state
ii) if you have a 1000 users and they all store 1MB in their sessions that's 1 GB of memory that you need
iii) If you are running on a cluster of servers then you need to serialize sessions across server boundaries, the more data there is the longer that will take


Kevin Jones<br />Author: <a href="http://www.amazon.com/exec/obidos/tg/detail/-/0321136497/jranch-20" target="_blank" rel="nofollow">Servlets and JSP: The J2EE Web Tier</a>
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
I have heard people compalining about performance because session is too heavy ...what does that actually mean then ?

True that the session only stores a reference, but the actual object has to live somewhere. As Kevin said, if each user causes creation of a new 1MB object you have a serious memory limitation or serialization overhead.
On the other hand, if the reference is to an object that is shared by a lot of users - an XML DOM representing a catalog for example - then having a reference to a 1MB object in a session is no big deal.
Bill
Phil Chuang
Ranch Hand

Joined: Feb 15, 2003
Posts: 251
Ok, say 1000 sessions have a reference to the same 1MB object (in the servlet context) - if those sessions are serialized, is the object itself serialized 1000 times or merely the reference to the object?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

With the session you also need to worry about longevity. Since the session could potentially stay around a long time, objects that you maintain references to from the session will not be subject to garbage collection and could build up over time. When using the session you must consciously be careful to release the references when they are no longer needed.
Contrast this to 'placing' objects on a request: since the request goes out of scope quickly, you generally don't need to worry about releasing attributes.
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Kevin Jones
Author
Ranch Hand

Joined: Oct 29, 2003
Posts: 39
Ok, say 1000 sessions have a reference to the same 1MB object (in the servlet context) - if those sessions are serialized, is the object itself serialized 1000 times or merely the reference to the object?

But why would you do this? If you have a shared object then you are either going to save it in a ServletContext, database or some other external storage
Phil Chuang
Ranch Hand

Joined: Feb 15, 2003
Posts: 251
Originally posted by Kevin Jones:

But why would you do this? If you have a shared object then you are either going to save it in a ServletContext, database or some other external storage

I'm not actually doing this, I'm just trying to understand the mechanism behind it all. It was just an example. My real question, sans example, is:
When sessions are serialized by the container,
1) is the reference or the object stored in the session?
2) if the same object is referenced by multiple sessions, is it saved once per session, or once for all of them?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
The principle behind serialization is to be able to completely reconstitute the object - in this case a collection of objects in a session. A serialized session can be rebuilt if the server has been restarted. Each session has to be independent of all others so it has to contain all referenced objects.
Think about it - how could you serialize just a reference, it has to be a complete object.
Bill
Phil Chuang
Ranch Hand

Joined: Feb 15, 2003
Posts: 251
Well I was wondering if there could be any behind the scenes kind of optimization - such that if there were some sort of occurence where all sessions were serialized, that the server would be smart enough to know how to not store mutliple redundant copies, some way of doing persistent references or something. Ah well, not really all that important, other than more reasons to keep the session small
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Basic question