Saturday, September 10, 2005

Yahoo, a sizeof function in Java

Finally, a possible addition to your logging arsenal.

A better way of calculating the size of objects since the early days of do-it-yourself-pseudo-calculations and using profilers is already part of Tiger (JDK 5.0).

With JDK 5.0, you can get size in two new ways without using expensive do-less-good profilers (why I say less is because, the programmer is still the best profiler) or native runtime-engine specific bridges:

java.lang.instrument.Instrumentation.getObjectSize API.

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.

getObjectSize() returns an implementation-specific approximation of the amount of storage consumed by the specified object. The result may include some or all of the object's overhead, and thus is useful for comparison within an implementation but not between implementations. The estimate may change during a single invocation of the JVM.


richard@work

No comments: