Class Data Sharing

Class Data Sharing

1
2
3
4
5
6
7
8

public class HelloCDS {
public static void main(String[] args) {
System.out.println("Hello CDS");
}
}


간단한 java 파일로 테스트

1
2
java -Xshare:dump

위 명령어를 쓰면 아래의 classlist 파일(기본 클래스 목록) 과 classes.jsa 아카이브가 생성이 된다.

1
2
3
4
vi /Library/Java/JavaVirtualMachines/adoptopenjdk-11.0.2.jdk/Contents/Home/classlist

ls /Library/Java/JavaVirtualMachines/adoptopenjdk-11.0.2.jdk/Contents/Home/lib/server/classes.jsa

아카이브가 생성 되 있지 않으면 -Xshare:on 옵션으로 실행하면 종료가 된다

1
2
3
4
rm -f /Library/Java/JavaVirtualMachines/adoptopenjdk-11.0.2.jdk/Contents/Home/lib/server/classes.jsa

java -Xshare:on HelloCDS

오류 메시지

1
2
3
4
5
6
7
 ~  rm -f /Library/Java/JavaVirtualMachines/adoptopenjdk-11.0.2.jdk/Contents/Home/lib/server/classes.jsa
~  java -Xshare:on HelloCDS
An error has occurred while processing the shared archive file.
Specified shared archive not found.
Error occurred during initialization of VM
Unable to use shared archive.

아카이브를 다시만들면

1
2
3
4
java -Xshare:dump

java -Xshare:on HelloCDS

실행

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
~  java -Xshare:dump
narrow_klass_base = 0x0000000800000000, narrow_klass_shift = 3
Allocated temporary class space: 1073741824 bytes at 0x00000008c0000000
Allocated shared space: 3221225472 bytes at 0x0000000800000000
Loading classes to share ...
Loading classes to share: done.
Rewriting and linking classes ...
Rewriting and linking classes: done
Number of classes 1222
instance classes = 1162
obj array classes = 52
type array classes = 8
Updating ConstMethods ... done.
Removing unshareable information ... done.
Scanning all metaspace objects ...
Allocating RW objects ...
Allocating RO objects ...
Relocating embedded pointers ...
Relocating external roots ...
Dumping symbol table ...
Dumping objects to closed archive heap region ...
Dumping objects to open archive heap region ...
Relocating SystemDictionary::_well_known_klasses[] ...
Removing java_mirror ... done.
mc space: 8104 [ 0.0% of total] out of 8192 bytes [ 98.9% used] at 0x0000000800000000
rw space: 3930240 [ 21.6% of total] out of 3932160 bytes [100.0% used] at 0x0000000800002000
ro space: 7198872 [ 39.6% of total] out of 7200768 bytes [100.0% used] at 0x00000008003c2000
md space: 2560 [ 0.0% of total] out of 4096 bytes [ 62.5% used] at 0x0000000800aa0000
od space: 6388936 [ 35.2% of total] out of 6389760 bytes [100.0% used] at 0x0000000800aa1000
st0 space: 425984 [ 2.3% of total] out of 425984 bytes [100.0% used] at 0x00000007bff00000
oa0 space: 212992 [ 1.2% of total] out of 212992 bytes [100.0% used] at 0x00000007bfe00000
total : 18167688 [100.0% of total] out of 18173952 bytes [100.0% used]
~  java -Xshare:on HelloCDS
Hello CDS

-Xlog:class+load 옵션으로 로그를 볼수 있다.

1
2
java -Xshare:on -Xlog:class+load HelloCDS

shared objects file를 포함하지 않은 모른 항목을 아래 처럼 볼수 있다.

1
2
java -Xshare:on -Xlog:class+load HelloCDS | grep --invert-match "shared objects file"

1
2
3
4
5
6
7
 ~  java -Xshare:on -Xlog:class+load HelloCDS | grep --invert-match "shared objects file"

[0.005s][info][class,load] opened: /Library/Java/JavaVirtualMachines/adoptopenjdk-11.0.2.jdk/Contents/Home/lib/modules
[0.050s][info][class,load] jdk.internal.loader.URLClassPath$FileLoader source: jrt:/java.base
[0.050s][info][class,load] jdk.internal.loader.URLClassPath$FileLoader$1 source: jrt:/java.base
[0.056s][info][class,load] HelloCDS source: file:/Users/sanaes/

위처럼 몇개의 항목이 직접 로드되는 클래스가 있다.

아래 처럼 cls 파일을 덤프 해서 처리 할수 있다.

1
2
3
4
java -XX:DumpLoadedClassList=/tmp/HelloCDS.cls HelloCDS

java -XX:SharedClassListFile=/tmp/HelloCDS.cls -XX:+UnlockDiagnosticVMOptions -XX:SharedArchiveFile=/tmp/HelloCDS.jsa -Xshare:dump

아래의 항목을 실행 해보면 모든 클래스가 로드 되어 있다.

1
2
3

java -Xshare:on -Xlog:class+load -XX:+UnlockDiagnosticVMOptions -XX:SharedArchiveFile=/tmp/HelloCDS.jsa HelloCDS | grep --invert-match "shared objects file"

실행결과

1
2
3
4
5
 ~  java -Xshare:on -Xlog:class+load -XX:+UnlockDiagnosticVMOptions -XX:SharedArchiveFile=/tmp/HelloCDS.jsa HelloCDS | grep --invert-match "shared objects file"

[0.005s][info][class,load] opened: /Library/Java/JavaVirtualMachines/adoptopenjdk-11.0.2.jdk/Contents/Home/lib/modules
[0.734s][info][class,load] HelloCDS source: file:/Users/sanaes/
Hello CDS
1
2
3
4
time java -Xshare:off -XX:+UnlockDiagnosticVMOptions -XX:SharedArchiveFile=/tmp/HelloCDS.jsa HelloCDS

time java -Xshare:on -XX:+UnlockDiagnosticVMOptions -XX:SharedArchiveFile=/tmp/HelloCDS.jsa HelloCDS

참조