且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Runtime.getRuntime()。maxMemory()计算方法

更新时间:2022-06-25 22:05:15

在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.