public static long sizeof(Class clazz,boolean roundResult){
  Assert.assertTrue(!clazz.isArray());
  long size;
  if (unsafe != null) {
    Field lastField=null;
    long lastFieldOffset=0;
    do {
      Field[] fields=clazz.getDeclaredFields();
      for (      Field field : fields) {
        if (!Modifier.isStatic(field.getModifiers())) {
          long offset=unsafe.fieldOffset(field);
          if (offset >= lastFieldOffset) {
            lastFieldOffset=offset;
            lastField=field;
          }
        }
      }
      if (lastField != null) {
        break;
      }
      clazz=clazz.getSuperclass();
    }
 while (clazz != null);
    if (lastField != null) {
      size=lastFieldOffset + sizeType(lastField.getType());
    }
 else {
      size=OBJECT_SIZE;
    }
  }
 else {
    size=OBJECT_SIZE;
    do {
      Field[] fields=clazz.getDeclaredFields();
      for (      Field field : fields) {
        if (!Modifier.isStatic(field.getModifiers())) {
          size+=sizeType(field.getType());
        }
      }
      clazz=clazz.getSuperclass();
    }
 while (clazz != null);
  }
  if (roundResult) {
    size=roundUpSize(size);
  }
  return size;
}

public static long sizeof ( Class clazz , boolean roundResult ) { Assert . assertTrue ( ! clazz . isArray ( ) ) ; long size ; if ( unsafe != null ) { Field lastField = null ; long lastFieldOffset = 0 ; do { Field [ ] fields = clazz . getDeclaredFields ( ) ; for ( Field field : fields ) { if ( ! Modifier . isStatic ( field . getModifiers ( ) ) ) { long offset = unsafe . fieldOffset ( field ) ; if ( offset >= lastFieldOffset ) { lastFieldOffset = offset ; lastField = field ; } } } if ( lastField != null ) { break ; } clazz = clazz . getSuperclass ( ) ; } while ( clazz != null ) ; if ( lastField != null ) { size = lastFieldOffset + sizeType ( lastField . getType ( ) ) ; } else { size = OBJECT_SIZE ; } } else { size = OBJECT_SIZE ; do { Field [ ] fields = clazz . getDeclaredFields ( ) ; for ( Field field : fields ) { if ( ! Modifier . isStatic ( field . getModifiers ( ) ) ) { size += sizeType ( field . getType ( ) ) ; } } clazz = clazz . getSuperclass ( ) ; } while ( clazz != null ) ; } if ( roundResult ) { size = roundUpSize ( size ) ; } return size ; }
since unsafe . fieldoffset ( field ) will give us the offset to the first byte of that field all we need to do is find which of the non - static declared fields has the greatest offset .
