Java CDS 类数据共享 Class Data Sharing

Java About 5,514 words

作用

类数据共享。

减少启动时间,降低内存占用。

注意:必须由多个JVM

文件位置

  • Linux/macOS: $JAVA_HOME/lib/server/classes.jsa
  • Windows: $JAVA_HOME/bin/server/classes.jsa

适用场景

多个JVM进程,共享同一个jsa文件。

Kubernetes中需挂载PV

参数值

  • -Xshare:off: 关闭
  • -Xshare:on: 打开,
  • -Xshare:auto: 默认值

支持的 GC

共享Java堆对象仅支持非Windows64位平台。

Java 8/Java 11只支持SerialGC

Java 12开始支持如下GC

  • ZGC
  • G1GC
  • SerialGC
  • ParallelGC

查看 JVM 参数

默认CDS是开启的,UseSharedSpacesture

bash-4.4$ java -XX:+PrintFlagsFinal -version | grep Share
NOTE: Picked up JDK_JAVA_OPTIONS: -XX:NativeMemoryTracking=detail -XX:+UseG1GC
     bool DumpSharedSpaces                         = false                                     {product} {default}
    ccstr ExtraSharedClassListFile                 =                                           {product} {default}
     bool PerfDisableSharedMem                     = true                                      {product} {default}
     bool PrintSharedArchiveAndExit                = false                                     {product} {default}
     bool PrintSharedDictionary                    = false                                     {product} {default}
     bool RequireSharedSpaces                      = false                                     {product} {default}
    ccstr SharedArchiveConfigFile                  =                                           {product} {default}
    ccstr SharedArchiveFile                        =                                           {product} {default}
   size_t SharedBaseAddress                        = 34359738368                               {product} {default}
    ccstr SharedClassListFile                      =                                           {product} {default}
    uintx SharedSymbolTableBucketSize              = 4                                         {product} {default}
     bool UseSharedSpaces                          = true                                      {product} {default}
     bool VerifySharedSpaces                       = false                                     {product} {default}
openjdk version "11.0.16" 2022-07-19
OpenJDK Runtime Environment 18.9 (build 11.0.16+8)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.16+8, mixed mode, sharing)

指定off后查看,可以看到UseSharedSpacesfalse,并且是command line指定。

bash-4.4$ java -Xshare:off -XX:+PrintFlagsFinal -version | grep Share
NOTE: Picked up JDK_JAVA_OPTIONS: -XX:NativeMemoryTracking=detail -XX:+UseG1GC
     bool DumpSharedSpaces                         = false                                     {product} {default}
    ccstr ExtraSharedClassListFile                 =                                           {product} {default}
     bool PerfDisableSharedMem                     = true                                      {product} {default}
     bool PrintSharedArchiveAndExit                = false                                     {product} {default}
     bool PrintSharedDictionary                    = false                                     {product} {default}
     bool RequireSharedSpaces                      = false                                     {product} {command line}
    ccstr SharedArchiveConfigFile                  =                                           {product} {default}
    ccstr SharedArchiveFile                        =                                           {product} {default}
   size_t SharedBaseAddress                        = 34359738368                               {product} {default}
    ccstr SharedClassListFile                      =                                           {product} {default}
    uintx SharedSymbolTableBucketSize              = 4                                         {product} {default}
     bool UseSharedSpaces                          = false                                     {product} {command line}
     bool VerifySharedSpaces                       = false                                     {product} {default}
openjdk version "11.0.16" 2022-07-19
OpenJDK Runtime Environment 18.9 (build 11.0.16+8)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.16+8, mixed mode)

查看是否开启 CDS

jcmd 1 VM.info | grep CDS

输出

bash-4.4$ jcmd 1 VM.info | grep CDS
CDS: on

查看 NMT

java -Xms600M -Xmx600M -XshowSettings:vm -XX:+PrintCommandLineFlags -XX:NativeMemoryTracking=summary -XX:+UnlockDiagnosticVMOptions -XX:+PrintNMTStatistics -version

输出:不管堆内存大小多少,Shared class space都为11177984字节(10M)。

bash-4.4$ java -Xms600M -Xmx600M -XshowSettings:vm -XX:+PrintCommandLineFlags -XX:NativeMemoryTracking=summary -XX:+UnlockDiagnosticVMOptions -XX:+PrintNMTStatistics -version
-XX:G1ConcRefinementThreads=1 -XX:GCDrainStackTargetSize=64 -XX:InitialHeapSize=629145600 -XX:MaxHeapSize=629145600 -XX:NativeMemoryTracking=summary -XX:+PrintCommandLineFlags -XX:+PrintNMTStatistics -XX:ReservedCodeCacheSize=251658240 -XX:+SegmentedCodeCache -XX:+UnlockDiagnosticVMOptions -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseG1GC 
VM settings:
    Min. Heap Size: 600.00M
    Max. Heap Size: 600.00M
    Using VM: OpenJDK 64-Bit Server VM

Native Memory Tracking:

Total: reserved=2055066876, committed=717432060
-                 Java Heap (reserved=629145600, committed=629145600)
                            (mmap: reserved=629145600, committed=629145600) 

-        Shared class space (reserved=11177984, committed=11177984)
                            (mmap: reserved=11177984, committed=11177984) 

参考

Java 17

https://docs.oracle.com/en/java/javase/17/vm/class-data-sharing.html

Java 8

https://docs.oracle.com/javase/8/docs/technotes/guides/vm/class-data-sharing.html

Views: 573 · Posted: 2023-12-11

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh