Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Basic question

 
Kavita Saluja
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Kevin Jones
Author
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic