Occasionally, you may be interested in measuring (or estimating) the size of an object in memory.
Here's a utility which can help with that task, if the object's class has a no-argument constructor.
It follows the style used by Java Platform Performance, by Wilson and Kesselman.
Given a class name, it will build a number of objects using the no-argument constructor, and measure the
effect on JVM memory use.
Thus, it measures the size of 'empty' objects containing no data.

To measure the size of a particular object containing data, a similar technique can easily be used:
measure JVM memory use before and after building the object.

In addition, JDK 1.5 has added an
Instrumentation
interface, which
includes a method named getObjectSize method.
However, this method seems to be intended for tool makers.