wood burning stoves 2.0*
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


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Session Information" Watch "Session Information" New topic
Author

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
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
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


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

Joined: Mar 22, 2000
Posts: 12769
    
    5
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".
Bill
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30392
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, 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
Greenhorn

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

import java.io.*;
public class SessionServlet extends javax.servlet.http.HttpServlet {
public void doGet (javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)
{

try{

javax.servlet.http.HttpSession sess = req.getSession();
sess.setAttribute("key","value");
java.io.FileOutputStream fout = new java.io.FileOutputStream("c:\\test");
ObjectOutputStream oout = new ObjectOutputStream(fout);
oout.writeObject(sess);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Session Information