JOL 22 Promotion


本篇文章基于V0.16 JOLSample_22_Promotion

本例用来演示对象提升。

一旦对象在GC过程中幸存下来,它就会被提升到另一代。在本例中,通过不停地GC,我们可以跟踪到对象的不停地址。

VM需要把每个对象的年龄(也就是对象通过GC存活下来的次数)记录在mark word上。下面通过例子我们仔细看一下每个对象GC后它的mark word值变化。

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
public class JOLSample_22_Promotion {

static volatile Object sink;

public static void main(String[] args) {
out.println(VM.current().details());

PrintWriter pw = new PrintWriter(System.out, true);

Object o = new Object();

ClassLayout layout = ClassLayout.parseInstance(o);

long lastAddr = VM.current().addressOf(o);
pw.printf("*** Fresh object is at %x%n", lastAddr);
out.println(layout.toPrintable());

int moves = 0;
for (int i = 0; i < 100000; i++) {
long cur = VM.current().addressOf(o);
if (cur != lastAddr) {
moves++;
pw.printf("*** Move %2d, object is at %x%n", moves, cur);
out.println(layout.toPrintable());
lastAddr = cur;
}

// make garbage
for (int c = 0; c < 10000; c++) {
sink = new Object();
}
}

long finalAddr = VM.current().addressOf(o);
pw.printf("*** Final object is at %x%n", finalAddr);
out.println(layout.toPrintable());

pw.close();
}

}

运行结果

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
84
85
86
87
88
89
90
# Running 64-bit HotSpot VM.
# Using compressed oop with 3-bit shift.
# Using compressed klass with 3-bit shift.
# WARNING | Compressed references base/shifts are guessed by the experiment!
# WARNING | Therefore, computed addresses are just guesses, and ARE NOT RELIABLE.
# WARNING | Make sure to attach Serviceability Agent to get the reliable addresses.
# Objects are 8 bytes aligned.
# Field sizes by type: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]
# Array element sizes: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]

*** Fresh object is at 76bd8a388
java.lang.Object object internals:
OFF SZ TYPE DESCRIPTION VALUE
0 8 (object header: mark) 0x0000000000000005 (biasable; age: 0)
8 4 (object header: class) 0xf80001e5
12 4 (object alignment gap)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

*** Move 1, object is at 76eb28000
java.lang.Object object internals:
OFF SZ TYPE DESCRIPTION VALUE
0 8 (object header: mark) 0x000000000000000d (biasable; age: 1)
8 4 (object header: class) 0xf80001e5
12 4 (object alignment gap)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

*** Move 2, object is at 76f5a8058
java.lang.Object object internals:
OFF SZ TYPE DESCRIPTION VALUE
0 8 (object header: mark) 0x0000000000000015 (biasable; age: 2)
8 4 (object header: class) 0xf80001e5
12 4 (object alignment gap)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

*** Move 3, object is at 76eb270d0
java.lang.Object object internals:
OFF SZ TYPE DESCRIPTION VALUE
0 8 (object header: mark) 0x000000000000001d (biasable; age: 3)
8 4 (object header: class) 0xf80001e5
12 4 (object alignment gap)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

*** Move 4, object is at 773588f40
java.lang.Object object internals:
OFF SZ TYPE DESCRIPTION VALUE
0 8 (object header: mark) 0x0000000000000025 (biasable; age: 4)
8 4 (object header: class) 0xf80001e5
12 4 (object alignment gap)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

*** Move 5, object is at 772b201d0
java.lang.Object object internals:
OFF SZ TYPE DESCRIPTION VALUE
0 8 (object header: mark) 0x000000000000002d (biasable; age: 5)
8 4 (object header: class) 0xf80001e5
12 4 (object alignment gap)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

*** Move 6, object is at 77ad80a78
java.lang.Object object internals:
OFF SZ TYPE DESCRIPTION VALUE
0 8 (object header: mark) 0x0000000000000035 (biasable; age: 6)
8 4 (object header: class) 0xf80001e5
12 4 (object alignment gap)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

*** Move 7, object is at 6c000cef8
java.lang.Object object internals:
OFF SZ TYPE DESCRIPTION VALUE
0 8 (object header: mark) 0x0000000000000035 (biasable; age: 6)
8 4 (object header: class) 0xf80001e5
12 4 (object alignment gap)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

*** Final object is at 6c000cef8
java.lang.Object object internals:
OFF SZ TYPE DESCRIPTION VALUE
0 8 (object header: mark) 0x0000000000000035 (biasable; age: 6)
8 4 (object header: class) 0xf80001e5
12 4 (object alignment gap)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total