File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Serialised session size Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Serialised session size" Watch "Serialised session size" New topic

Serialised session size

Steve Granton
Ranch Hand

Joined: Jan 13, 2002
Posts: 200
If I serialise a http session to obtain the size in bytes how accurate is it? Is the true size of the session in the app server likely to be much different to the size seen when serialised?
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
The size of the serialized object should be indicative of the in-memory object, but note that if there are a lot of Strings in the session, the memory will be taking 2 bytes per character.
Erick Jones
Ranch Hand

Joined: Jun 17, 2002
Posts: 38
Here is the how to determine the size of your persistent http session. Place this code in the doGet/doPost:
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream stream = new ObjectOutputStream(out);
System.out.println("Total Size: " + out.size + "bytes");
Erick Jones
Ranch Hand

Joined: Jun 17, 2002
Posts: 38
I may have misunderstood the question. Are you trying to figure out the peristent size, or the in-memory size? They are not the same. The persistent size of an object is much larger than the memory it consumes.
My previous post assumes you are interested in the persistent size. If you want to know the in memory size it is not quite that simple, but here is an article that I found that will allow you to figure it out.
Steve Granton
Ranch Hand

Joined: Jan 13, 2002
Posts: 200
We've been experiencing Out of Memory errors with Websphere 4.0 and I am trying to establish the size of the session at various points in the application - so I guess I'm interested in the in memory session size - I was hoping that the serialised session size will be indicative of the in memory size but it appears not. I'll take a look at the article you suggested.
Thanks for the help,
Stuart Bell

Joined: Sep 17, 2004
Posts: 22
I know this is an old topic, but the link to the article cannot be found now. Does anyone have any information on how to calculate the in-memory session size?



SCJP (Java 2)<br />SCJD<br />SCWCD
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

This isn't that easy to do because several sessions could hold references to the same object.

One approach would be to use a profiler such as JProbe, or Optimizeit and measure the overall memory footprint as sessions are added.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
I agree. Here's the link:
subject: Serialised session size
It's not a secret anymore!