为什么 MaxHeapSize 值在 java -XX:+PrintFlagsFinal 和 jinfo -flag MaxHeapSize 之间不同

why the MaxHeapSize values are different between java -XX:+PrintFlagsFinal and jinfo -flag MaxHeapSize

我 运行 我的 java 应用程序在 docker 容器中的 apline linux 系统上,我想找出 MaxHeapSize 的值,所以我使用了几个命令:java -XX:+PrintFlagsFinal,jinfo -flag MaxHeapSize,jmap -heap,但输出让我感到困惑。的输出 jinfo -flag MaxHeapSize , jmap -heap 是一致的。但是,java -XX:+PrintFlagsFinal 的输出是 different.so 为什么会这样?

默认容器内存限制设置为 4096MiB。

enter image description here

java commonds的输出如下图(我在图中标出了一些重要的部分)

bash-5.0# jps -v
9 jar -Dfile.encoding=utf-8 -XX:+UseG1GC -XX:+UseStringDeduplication -XX:-OmitStackTraceInFastThrow -XX:MaxRAMPercentage=60.0 -XX:InitialRAMPercentage=20.0 -XX:+PrintTenuringDistribution -XX:+PrintGCDetails -XX:+PrintCommandLineFlags -XX:+PrintHeapAtGC -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -Xloggc:log/gc-%t.log -Duser.timezone=Asia/Shanghai -Delastic.apm.service_name=SUPER-STUDENTS -Delastic.apm.environment=k8s-prod-th-zhidao-manhattan -Delastic.apm.server_urls= -Delastic.apm.trace_methods= -Delastic.apm.trace_methods_duration_threshold=100ms -Delastic.apm.application_packages=outfox -Delastic.apm.capture_body=all -Delastic.apm.ignore_message_queues=* -Delastic.apm.profiling_inferred_spans_enabled=true -Delastic.apm.profiling_inferred_spans_sampling_interval=10ms -Delastic.apm.profiling_inferred_spans_min_duration=50ms -Dskywalking.agent.service_name=super-students -Dskywalking.agent.instance_name=super-students-75f964dbbd-5gfnv -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=1234
64155 Jps -Dapplication.home=/opt/java/openjdk -Xms8m
bash-5.0# java -XX:+PrintFlagsFinal -version | grep -Ei "maxheapsize|maxram"
    uintx DefaultMaxRAMFraction                     = 4                                   {product}
    uintx MaxHeapSize                              := 1073741824                          {product}
 uint64_t MaxRAM                                    = 137438953472                        {pd product}
    uintx MaxRAMFraction                            = 4                                   {product}
   double MaxRAMPercentage                          = 25.000000                           {product}
openjdk version "1.8.0_282"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_282-b08)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.282-b08, mixed mode)
bash-5.0# jinfo -flag MaxHeapSize 9
-XX:MaxHeapSize=2577399808
bash-5.0# jmap -heap 9
Attaching to process ID 9, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.282-b08

using thread-local object allocation.
Garbage-First (G1) GC with 1 thread(s)

Heap Configuration:
   MinHeapFreeRatio         = 40
   MaxHeapFreeRatio         = 70
   MaxHeapSize              = 2577399808 (2458.0MB)
   NewSize                  = 1363144 (1.2999954223632812MB)
   MaxNewSize               = 1545601024 (1474.0MB)
   OldSize                  = 5452592 (5.1999969482421875MB)
   NewRatio                 = 2
   SurvivorRatio            = 8
   MetaspaceSize            = 21807104 (20.796875MB)
   CompressedClassSpaceSize = 1073741824 (1024.0MB)
   MaxMetaspaceSize         = 17592186044415 MB
   G1HeapRegionSize         = 1048576 (1.0MB)

Heap Usage:
G1 Heap:
   regions  = 2458
   capacity = 2577399808 (2458.0MB)
   used     = 320120112 (305.2903289794922MB)
   free     = 2257279696 (2152.709671020508MB)
   12.420273758319455% used
G1 Young Generation:
Eden Space:
   regions  = 53
   capacity = 654311424 (624.0MB)
   used     = 55574528 (53.0MB)
   free     = 598736896 (571.0MB)
   8.493589743589743% used
Survivor Space:
   regions  = 10
   capacity = 10485760 (10.0MB)
   used     = 10485760 (10.0MB)
   free     = 0 (0.0MB)
   100.0% used
G1 Old Generation:
   regions  = 247
   capacity = 389021696 (371.0MB)
   used     = 254059824 (242.2903289794922MB)
   free     = 134961872 (128.7096710205078MB)
   65.30736630174992% used

63962 interned Strings occupying 6772928 bytes.

enter image description here

这些不是在比较同一件事。

当 运行ning jmapjstack 时,这些附加到 PID 为 9 的现有进程,如第一个 jps 命令中所列。

当 运行ning java -XX:+PrintFlagsFinal -version 时,这将创建一个新的 JVM 进程,并打印该新进程的信息。请注意,原始 PID 9 进程有许多额外的标志,这些标志会影响计算出的堆大小。

为了更准确的比较,您可以在容器启动时将 -XX:+PrintFlagsFinal 标志添加到主命令 运行 中。我希望这与 jinfojmap.

返回的值匹配