views:

474

answers:

4

Is there a clean and easy way to measure it other than programatically measuring the size of each data type (that each object is composed of) stored in session?

A: 

Can Lambda Probe do what you need? It seems to have a few tools for monitoring memory and session usage.

jsight
None of the profiling tools I have used give the size. I am not sure about Lambda Probe.
Langali
+1  A: 

There's a SessionSize class here that has a function to return the size of an HttpSession object passed to it, a part of the Java Web Parts lib.

kchau
+1  A: 

Session content must be Serializable. So serialize it and see the size of the resulting byte array.

It's not equal to the in-memory size, but can be used as a rough representation of it.

P.S. Note that transient fields, if any, will be excluded.

Vladimir Dyuzhev
Session attributes **should** be Serializable (as in "it's a good practice to do so"); but they don't **have** to be.
ChssPly76
Session serialization is an effective way to save memory by saving idle sessions to disk. If Erlanged has app with non-serializable session, it's the first thing to fix instead of measuring induvidual sessions' size.
Vladimir Dyuzhev
Re: must/should: container MUST accept Serializable object, and MAY accept others. It MAY also throw IllegalArgumentException on any non-serializable one. I'd say Serializable is pretty much MUST, in a WE-TOLD-YOU-SO way.
Vladimir Dyuzhev
But technically you're correct, of course. :)
Vladimir Dyuzhev
+2  A: 

MessAdmin allows you to compute the HttpSession size although it is unclear on how it calculates the size of transient objects.

It appears that getting an approximate size of the HttpSession object is an exercise in futility in production, and one is likely to get a more accurate size for a controlled environment.

One thing to note is that size of the serialized session object is bound to inaccurate due to changes in character encoding - Strings in Java are stored in the UTF-16 format whereas the output stream could be in a different encoding. More details on why calculating the size of an object in Java is a problem, can be found in this JavaWorld article.

Vineet Reynolds