本文介绍了Runtime.getRuntime()。maxMemory()计算方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是代码:

    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上运行代码是:

Run the Code on JDK8 is :


    [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 max: 954728448 (910.50 M) *

Runtime.maxMemory是910.50M,我想知道这是如何工作的

The Runtime.maxMemory is 910.50M, I want to know how this works out


,但它不适用于JDK8。

On JDK7, "Runtime.getRuntime().maxMemory()" = "-Xmx" - "Survivor" , But it does not work on JDK8。

推荐答案

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

In JDK 8 the formula Runtime.maxMemory() = Xmx - Survivor is still fair, but the trick is how Survivor is estimated.

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

You haven't set the initial heap size (-Xms), and the Adaptive Size Policy is on by default. This means the heap can resize and heap generation boundaries can move in runtime. Runtime.maxMemory() estimates the amount of memory conservatively, subtracting the maximum possible survivor size from the size of New Generation.

Runtime.maxMemory() = OldGen + NewGen - MaxSurvivor

  where MaxSurvivor = NewGen / MinSurvivorRatio

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

In your example OldGen = 683 MB, NewGen = 341 MB and MinSurvivorRatio = 3 by default. That is,

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

如果禁用 -XX:-UseAdaptiveSizePolicy 或设置初始堆size -Xms -Xmx 相同的值,你会再次看到运行时.maxMemory()= OldGen + Eden + Survivor

If you disable -XX:-UseAdaptiveSizePolicy or set the initial heap size -Xms to the same value as -Xmx, you'll see again that Runtime.maxMemory() = OldGen + Eden + Survivor.

这篇关于Runtime.getRuntime()。maxMemory()计算方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 16:59