0

I want to measure the size of an object (Serialized or not) when it is set to HttpSession. How can I do this ? Even some supporting questions found, it could not be used.. Any one know how I can do this ? Probably, usage of session listener could be valuable.But how to measure the size of the Object ? For example

session.setAttribute("attrName",sessionObj); 

How to measure the size of this sessionObj ?

Débora
  • 5,816
  • 28
  • 99
  • 171

1 Answers1

1

You can use java.lang.instrument to measure size of an object.

This class provides services needed to instrument Java programming language code. Instrumentation is the addition of byte-codes to methods for the purpose of gathering data to be utilized by tools. Since the changes are purely additive, these tools do not modify application state or behavior. Examples of such benign tools include monitoring agents, profilers, coverage analyzers, and event loggers.

This answer is an example how to use it.

As you stated, [HttpSessionAttributeListener][3] is a good place for checking size of added attributes. Add here code for size.

public void attributeAdded(ServletContextAttributeEvent scae) {
    System.out.println("Attribute addedd:["+scae.getName()+"],"+scae.getValue());
}
Community
  • 1
  • 1
user987339
  • 10,519
  • 8
  • 40
  • 45
  • Thanks v much.. but one explanation for Instrumentation. how to culate the size of the object inside the method "getObjectSize()" of Instrumentation ? – Débora Jan 17 '14 at 11:21
  • http://stackoverflow.com/questions/52353/in-java-what-is-the-best-way-to-determine-the-size-of-an-object – user987339 Jan 17 '14 at 13:51