Search code examples
javaspring-bootjava-17

Dynamic Class Data Sharing not using the archive file


We are trying to use Dynamic Class Data Sharing (Dynamic CDS) with our spring boot application.

Created the CDS archive using the following command

java -Xlog:cds -XX:DumpLoadedClassList=cds.jsa -jar app-0.0.1-SNAPSHOT.jar

And trying to use the archive using the following command

java -Xlog:cds -XX:SharedArchiveFile=cds.jsa -jar app-0.0.1-SNAPSHOT.jar

But getting the following error in the beginning of the application startup

[0.009s][info][cds] trying to map cds.jsa
[0.009s][info][cds] Opened archive cds.jsa.
[0.009s][info][cds] _magic expected: 0xf00baba2
[0.009s][info][cds]          actual: 0x4f4e2023
[0.009s][info][cds] UseSharedSpaces: The shared archive file has a bad magic number.
[0.009s][info][cds] UseSharedSpaces: Unable to map shared spaces

openjdk version "17" 2021-09-14 (build 17+35-2724) Springboot 2.6.3


Solution

  • The option --XX:DumpLoadedClassList=cds.jsa does not create a CDS archive - it is one step in creating a static CDS Archive File.

    To create a Dynamic CDS Archive file you must use

    java -Xlog:cds -XX:ArchiveClassesAtExit=cds.jsa -jar app-0.0.1-SNAPSHOT.jar
    

    Source: https://docs.oracle.com/en/java/javase/17/docs/specs/man/java.html#creating-cds-archives