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

Session Information

Blikkies Marais
Ranch Hand

Joined: Jun 12, 2003
Posts: 50
Is it possible to determin what the size of a session is? I have a class that implements HttpSessionBindingListener, when the valueBound method is invoked, I want to determine what the size of the session is. Thanks in advance.
Kyle Brown
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
The easiest way to do that is to serialize the HttpSession object onto a ByteStream and then look at the size (byte position) of the resulting ByteStream. I used to have some code that did that somewhere -- I'll post it if I can find it...


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at for other WebSphere information.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13035
You can't get an estimate of the "size" of a session - if you mean the amount of memory the session occupies. A session can have references to all sorts of objects. Since the HttpSession object itself is just a collection plus some bookkeeping, an instance is pretty small.

The amount of memory that would be released when the session is invalidated depends on whether or not those objects can be GCed. A session that is holding on to references to lots of big objects - where those references are the only thing that prevents GC - might be considered "big".
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33098

When I measure the "size" of a session, I use Kyle's definition and go with how much memory would be needed if the session were persisted. (like between app servers in a cluster)

I agree that the session may be using less memory although I try to avoid pointing to non-session specific things from my session to keep the session small.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Blikkies Marais
Ranch Hand

Joined: Jun 12, 2003
Posts: 50
Thanks for your response, but please can you give a code example of how you do this (Serialize a HttpSession to a ByteStream). Also what api do you need for ByteStream?
shriram kutty

Joined: Jun 01, 2004
Posts: 3
The following servlet will write the session object to a file.

public class SessionServlet extends javax.servlet.http.HttpServlet {
public void doGet (javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)


javax.servlet.http.HttpSession sess = req.getSession();
sess.setAttribute("key","value"); fout = new"c:\\test");
ObjectOutputStream oout = new ObjectOutputStream(fout);
catch(Exception e)
I agree. Here's the link:
subject: Session Information
It's not a secret anymore!