JVM之Java对象模型(一)

oop-klass

klass

在JVM中,Klass是用来描述一个类的元数据的,里面包括了类的修饰符、类名、父类、类加载器等,简单来说就是描述一个Java类的元数据的数据结构。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
class Klass : public Metadata {
friend class VMStructs;
protected:
// note: put frequently-used fields together at start of klass structure
// for better cache behavior (may not make much of a difference but sure won't hurt)
enum { _primary_super_limit = 8 };

// The "layout helper" is a combined descriptor of object layout.
// For klasses which are neither instance nor array, the value is zero.
//
// For instances, layout helper is a positive number, the instance size.
// This size is already passed through align_object_size and scaled to bytes.
// The low order bit is set if instances of this class cannot be
// allocated using the fastpath.
//
// For arrays, layout helper is a negative number, containing four
// distinct bytes, as follows:
// MSB:[tag, hsz, ebt, log2(esz)]:LSB
// where:
// tag is 0x80 if the elements are oops, 0xC0 if non-oops
// hsz is array header size in bytes (i.e., offset of first element)
// ebt is the BasicType of the elements
// esz is the element size in bytes
// This packed word is arranged so as to be quickly unpacked by the
// various fast paths that use the various subfields.
//
// The esz bits can be used directly by a SLL instruction, without masking.
//
// Note that the array-kind tag looks like 0x00 for instance klasses,
// since their length in bytes is always less than 24Mb.
//
// Final note: This comes first, immediately after C++ vtable,
// because it is frequently queried.
jint _layout_helper;

// The fields _super_check_offset, _secondary_super_cache, _secondary_supers
// and _primary_supers all help make fast subtype checks. See big discussion
// in doc/server_compiler/checktype.txt
//
// Where to look to observe a supertype (it is &_secondary_super_cache for
// secondary supers, else is &_primary_supers[depth()].
juint _super_check_offset;

// Class name. Instance classes: java/lang/String, etc. Array classes: [I,
// [Ljava/lang/String;, etc. Set to zero for all other kinds of classes.
Symbol* _name;

// Cache of last observed secondary supertype
Klass* _secondary_super_cache;
// Array of all secondary supertypes
Array<Klass*>* _secondary_supers;
// Ordered list of all primary supertypes
Klass* _primary_supers[_primary_super_limit];
// java/lang/Class instance mirroring this class
oop _java_mirror;
// Superclass
Klass* _super;
// First subclass (NULL if none); _subklass->next_sibling() is next one
Klass* _subklass;
// Sibling link (or NULL); links all subklasses of a klass
Klass* _next_sibling;

// All klasses loaded by a class loader are chained through these links
Klass* _next_link;

// The VM's representation of the ClassLoader used to load this class.
// Provide access the corresponding instance java.lang.ClassLoader.
ClassLoaderData* _class_loader_data;

jint _modifier_flags; // Processed access flags, for use by Class.getModifiers.
AccessFlags _access_flags; // Access flags. The class/interface distinction is stored here.

// Biased locking implementation and statistics
// (the 64-bit chunk goes first, to avoid some fragmentation)
jlong _last_biased_lock_bulk_revocation_time;
markOop _prototype_header; // Used when biased locking is both enabled and disabled for this type
jint _biased_lock_revocation_count;

TRACE_DEFINE_KLASS_TRACE_ID;

// Remembered sets support for the oops in the klasses.
jbyte _modified_oops; // Card Table Equivalent (YC/CMS support)
jbyte _accumulated_modified_oops; // Mod Union Equivalent (CMS support)

这里就是Klass包含的Java类的大部分元数据

oop

oop全称Ordinary Object Pointer,翻译过来就是普通对象指针,在JVM中,oop对应的类是oopDesc,每new一个Java实例就会创建一个oopDesc,oopDesc包含了两部分信息,_mark和_metadata,markword是用来存储对象的锁信息和分代年龄用的,_metadata就是指向klass的一个指针,通过指针可以拿到这个类的元数据。

1
2
3
4
5
6
7
8
class oopDesc {
friend class VMStructs;
private:
volatile markOop _mark;
union _metadata {
Klass* _klass;
narrowKlass _compressed_klass;
} _metadata;

为什么要用oop-klass模型

One reason for the oop/klass dichotomy in the implementation is that we don’t want a C++ vtbl pointer in every object. Thus, normal oops don’t have any virtual functions. Instead, they forward all “virtual” functions to their klass, which does have a vtbl and does the C++ dispatch depending on the object’s actual type. (See oop.inline.hpp for some of the forwarding code.)

上面这段话是在Klass.cpp的注释中的,大概意思是在JVM中,设计者不想让每个对象都保存一份虚函数表,(ps:虚函数表主是要一个类的虚函数的地址表,这张表解决了继承、覆盖的问题,保证其容真实反应实际的函数。这样,在有虚函数的类的实例中这个表被分配在了这个实例的内存中,所以,当我们用父类的指针来操作一个子类的时候,这张虚函数表就显得由为重要了,它就像一个地图一样,指明了实际所应该调用的函数),所以JVM就把对象一分为二,用klass和oop来表示,klass用来存储对象的方法和其他元数据信息,oop用来存储对象的实例数据,也就是Java的对象头信息。

Java对象在内存中的布局

图中的Mark Word就是oop中的_mark,指向Class的指针就是oop中的Klass,这两部分合起来就叫Java对象头。对象的Body部分就是Java实例的实例变量,也就是我们在Java类中定义的成员变量。对齐字节简单理解就是,在JVM中,Java对象的大小都必须是8bytes的整数倍,所以当上述的实例变量的字节不是8的整数倍时,就要使用对齐字节来填充。
图1

java.lang.Class与Klass

1
2
// java/lang/Class instance mirroring this class
oop _java_mirror;

上面的这行代码是Klass中定义的,注释的意思就是_java_mirror这个实例表示java.lang.Class,在JVM加载类的时候,会创建一个oop的实例来表示Class实例,_java_mirror的Klass指针会指向真正的Klass,在调用Class.newInstance创建Java实例的时候,_java_mirror会找到真正的Klass完成实例的创建。至于为什么要用_java_mirror这个变量来表示java.lang.Class而不直接用Klass,根据我的理解,因为JVM用的是Klass-oop模型,所以对于java.lang.Class的实例来说也会分为Klass和oop,oop表示的是Class的对象头信息,klass包含了Class方法等元数据信息。
所以在Java中,访问一个对象的实例的过程就如下图所示:

Java对象在内存中的存储

这里的内存讨论是在JVM内存层面,不深入到操作系统内存层面。
一个Java对象在内存中的数据包含三个部分,对象头、实例数据、对齐填充。
对象头上面已经说过了,包括了Mark Work和对象指针;
实例数据就是Java类的成员变量,包括父类的成员变量;
对齐填充没有存储数据,起占位符的作用,因为在HotSpot VM中要求对象的起始地址是8的倍数,所以如果对象头加实例数据的大小不是8的倍数就要利用对齐填充来补充到8的倍数。

实验一

我的机器64位的,默认开启指针压缩的。
工具是JOL:

1
2
3
4
5
<dependency>
<groupId>org.openjdk.jol</groupId>
<artifactId>jol-core</artifactId>
<version>0.9</version>
</dependency>

可以看到objectHeader就是对象头,里面包括了Mark Work和Klass(对象指针),一共占了12字节,因为Mark Work占了8bytes,Klass压缩后占了4bytes,偏移量是0。成员变量a和b分别占了四个字节,a的偏移量是12,b的偏移量是16。object header + a + b的size就是20,不是8的整数倍,所以要加上对齐填充4bytes成为24bytes。

实验二

TestObject1继承Super后排在对象头后面的就是Super的成员变量,接下来才是TestObject1的成员变量,Super中有一个成员变量是Object类型,也是占了4bytes,存储的并不是Object的数据,而是Object的引用地址。