Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java EE - find session size

Is there a way to programatically track the size of a particular session on a Java EE 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.
like image 812
Ryan Fernandes Avatar asked Feb 28 '23 13:02

Ryan Fernandes


2 Answers

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](http://java.sun.com/j2se/1.5.0/docs/api/java/lang/instrument/Instrumentation.html#getObjectSize(java.lang.Object)). It requires you to start the JVM with special agent:

    java -javaagent:sizeofag.jar

like image 86
Grzegorz Oledzki Avatar answered Mar 08 '23 09:03

Grzegorz Oledzki


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.

like image 22
Gareth Davis Avatar answered Mar 08 '23 09:03

Gareth Davis