java.lang.instrument.Instrumentation.getObjectSize for java.lang.Class instance

Krystal Mok rednaxelafx at
Fri Dec 14 10:40:19 PST 2012

Hi Peter,

Which version of JDK are you using? Assuming you're using a JDK6/JDK7
version released to date, the actual implementation for
j.l.i.Instrumentation.getObjectSize() is as follows:

JvmtiEnv::GetObjectSize(jobject object, jlong* size_ptr) {
  oop mirror = JNIHandles::resolve_external_guard(object);

  if (mirror->klass() == SystemDictionary::Class_klass()) {
    if (!java_lang_Class::is_primitive(mirror)) {
        mirror = java_lang_Class::as_klassOop(mirror);
        assert(mirror != NULL, "class for non-primitive mirror must exist");

  *size_ptr = mirror->size() * wordSize;
} /* end GetObjectSize */

So there's special handling for j.l.Class instances' size: it's
reporting the size of the klassOop (the VM internal type that holds
the metadata). That's why changing the number of fields in j.l.Class
doesn't affect the result of getObjectSize(clazz).

(BTW, the code is a bit different after NoPermGen, but the idea stays the same.)


On Sat, Dec 15, 2012 at 2:18 AM, Peter Levart <peter.levart at> wrote:
> Hi,
> Does anybody know what the size returned by
> java.lang.instrument.Instrumentation.getObjectSize when passed a Class
> instance actually means? It is different for various Class instances
> (contains static fields of various classes?). It's also quite big (520 bytes
> for Object.class on 64bit JVM without compressed OOPS). If I remove some
> instance fields from java.lang.Class the reported size stays the same.
> Regards, Peter

More information about the hotspot-runtime-dev mailing list