tags:

views:

453

answers:

2

Is there a way to programatically track the size of a particular session on a JEE app server, or do I have to resort to the app server's vendor specific instrumentation to do this?

Two scenarios:

  1. Track from within the application (a sort of JMX-type interface)
  2. Track from without (outside) - a generic piece of code that works on all app servers.
+1  A: 

There isn't a standard way todo this. In fact there isn't actually a particularly good way to weigh an Object, assuming it's more than just primatives. One way to is to serialise the object to a byte array and take that as an indicator of the size.

An option would be to use a profiler like YourKit switch makes a pretty good stab at calculating the retained size of a reference.

There maybe Vendor specific API's for this as most SessionManager's have to serialise the session data for replication and persistence.

Gareth Davis
I was afraid that this might be the answer. I've been in situations where the server goes out of memory due to some wiseguy deciding to use the session as an attic.Anyway, I always used a profiler to do this before and I guess I'll be doing the same for some time to come. Thanks
Ryan Fernandes
+1  A: 

There are two approaches:

  • if your session is Serializable (as should be), there are some tools that calculate the length of the serialized version of the Session object. e.g. LambdaProbe for Apache Tomcat
  • if your session is not Serializable, then it's harder. The solution we've taken is to follow the sizeofagent technique. It makes use of the Instrumentation.getObjectSize() method. It requires you to start the JVM with special agent:

    java -javaagent:sizeofag.jar

Grzegorz Oledzki
Thanks for your suggestions on Tomcat, I'm sure there will be something for each App Server. But more importantly, thanks for the link to 'sizeofagent techniques'.
Ryan Fernandes
One thing I forgot to say about the 'sizeofagent technique'. If you used the 'fullSizeOf' method provided there, it would calculate the size of whole web of objects reachable from the Session object. That might be tons of data (incl. JavaEE server own objects, etc.). So what we've done is we calculated the session size just after it has been created and then calculated the difference - what has been added after.
Grzegorz Oledzki