gpt4 book ai didi

java-8 - Runtime.getRuntime().maxMemory() 计算方法

转载 作者:行者123 更新时间:2023-12-04 01:51:13 25 4
gpt4 key购买 nike

代码如下:

    System.out.println("Runtime max: " + mb(Runtime.getRuntime().maxMemory()));
MemoryMXBean m = ManagementFactory.getMemoryMXBean();

System.out.println("Non-heap: " + mb(m.getNonHeapMemoryUsage().getMax()));
System.out.println("Heap: " + mb(m.getHeapMemoryUsage().getMax()));

for (MemoryPoolMXBean mp : ManagementFactory.getMemoryPoolMXBeans()) {
System.out.println("Pool: " + mp.getName() +
" (type " + mp.getType() + ")" +
" = " + mb(mp.getUsage().getMax()));
}

在 JDK8 上运行代码是:

    [root@docker-runner-2486794196-0fzm0 docker-runner]# java -version    java version "1.8.0_181"    Java(TM) SE Runtime Environment (build 1.8.0_181-b13)    Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)    [root@docker-runner-2486794196-0fzm0 docker-runner]# java -jar -Xmx1024M -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap  test.jar    Runtime max: 954728448 (910.50 M)    Non-heap: -1 (-0.00 M)    Heap: 954728448 (910.50 M)    Pool: Code Cache (type Non-heap memory) = 251658240 (240.00 M)    Pool: Metaspace (type Non-heap memory) = -1 (-0.00 M)    Pool: Compressed Class Space (type Non-heap memory) = 1073741824 (1024.00 M)    Pool: PS Eden Space (type Heap memory) = 355467264 (339.00 M)    Pool: PS Survivor Space (type Heap memory) = 1048576 (1.00 M)    Pool: PS Old Gen (type Heap memory) = 716177408 (683.00 M)    

*最大运行时间:954728448 (910.50 M) *

Runtime.maxMemory是910.50M,我想知道这是怎么算的

On JDK7, "Runtime.getRuntime().maxMemory()" = "-Xmx" - "Survivor" , 但是在JDK8上是不行的。

最佳答案

在 JDK 8 中,公式 Runtime.maxMemory() = Xmx - Survivor 仍然是公平的,但诀窍在于如何估计 Survivor。

您尚未设置初始堆大小 (-Xms),自适应大小策略默认处于启用状态。这意味着堆可以调整大小并且堆生成边界可以在运行时移动。 Runtime.maxMemory() 保守估计内存量,从新生代的大小中减去最大可能幸存者大小。

Runtime.maxMemory() = OldGen + NewGen - MaxSurvivor

where MaxSurvivor = NewGen / MinSurvivorRatio

在您的示例中,默认情况下 OldGen = 683 MB,NewGen = 341 MB 和 MinSurvivorRatio = 3。也就是说,

Runtime.maxMemory() = 683 + 341 - (341/3) = 910.333 MB

如果您禁用 -XX:-UseAdaptiveSizePolicy 或将初始堆大小 -Xms 设置为与 -Xmx 相同的值,您您将再次看到 Runtime.maxMemory() = OldGen + Eden + Survivor

关于java-8 - Runtime.getRuntime().maxMemory() 计算方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52980629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com