Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Session Information

 
Blikkies Marais
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34179
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Blikkies Marais
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic