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:
There are two approaches:
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With