This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Session Size Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Session Size" Watch "Session Size" New topic
Author

Session Size

Farouk Sherefudheen
Ranch Hand

Joined: Jan 27, 2004
Posts: 32
Hello Friends
I use the following code to get a indication about the persistant usage of
Session Memory
Now my question is does the serialized bytes represent the size of references or the size of the session object or the reference to session


ByteArrayOutputStream out = null;
try{
out = new ByteArrayOutputStream();
ObjectOutputStream stream = new ObjectOutputStream(out);
stream.writeObject(request.getSession());
stream.flush();
}catch (IOException e){
log.error("Exception while retrieving size " + e);
}
log.debug("Total session size is : " + out.size() + "bytes");

Please reply
Thanks
Farouk
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

The session object is nothing more than a map of references to objects stored on the heap.

You will need to measure each of those objects separately.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
To serialize a Map, you have to serialize the name/value pairs contained within it, usually.


James Carman, President<br />Carman Consulting, Inc.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
A serialized session contains ALL of the referenced objects in serialized form, thus it may or may not have anything to do with memory useage associated with keeping a reference in a session.
If the only thing you are tracking in a session is an object that holds user specific data for a single user then the size of a serialized session has some relation to memory used but don't expect a precise number.
Just to take one example, suppose you keep a reference to a String that is also used in dozens of other users sessions. The String pool may have only one instance, but each serialized session will write the whole thing.
Bill
Farouk Sherefudheen
Ranch Hand

Joined: Jan 27, 2004
Posts: 32
Thanks for all your replies, if I understand correct it is the size of the references used in the session object and not the size of objects stored in the heap.

How can I find out the size of objects in heap, do I have to get each attribute from the session, find its type and then based on that calculate the size of heap?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Yes, but as William said, several sessions can be pointing to the same object which will make the results seem larger than they are.

Have you considered using a profiler (such as JProbe or Optimizeit) to measure the overall memory conumption as sessions are added from a test script?
Farouk Sherefudheen
Ranch Hand

Joined: Jan 27, 2004
Posts: 32
Thanks
I understand your concern, I am running this test from my pc inside wsad trying to get an estimate of the session size for a single user session,
Can you tell me what to do get the heap size associated with the session.
Any code samples would be nice..
Thanks
Farouk
Farouk Sherefudheen
Ranch Hand

Joined: Jan 27, 2004
Posts: 32
Also
I am using JPROBE and got an estimate of heap sizes. What i did was execute certain transactions right after begining the server till I have exected few use cases. Like add , search, view and update and delete use cases.
All of this i have done as one single stretch without requesting garbage collection and garbage collection happens when jvm wants. Do you think this is the right approach to get the heap sizes
Regards
Farouk
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Session Size
 
Similar Threads
Serialised session size
How to print out # of bytes used
Pass objects through form submit
size of the object in session
Wanted to pack values in java like struct.pack() in python