简体   繁体   English

堆大小大于 -Xmx

[英]Heap size is greater than -Xmx

I set the initial and maximum Java heap size to 256MB using -Xms256m and -Xmx256m JVM options .我使用-Xms256m-Xmx256m JVM options将初始和最大 Java 堆大小设置为 256MB。 The GC log (using -XX:+PrintHeapAtGC ) states that the heap size is 251904K (246MB), which is smaller than the minimum heap size, -Xms256m (see last line of log). GC 日志(使用-XX:+PrintHeapAtGC )指出堆大小为251904K (246MB),小于最小堆大小-Xms256m (请参阅日志的最后一行)。 However, this is because the stated heap size is the available heap size , which does not include the unavailable from space .但是,这是因为声明的堆大小是可用堆大小,不包括可用空间

When I manually include the unavailable from space memory, the derived heap size is 262656K (256.5MB), which is slightly larger than the maximum heap size, -Xmx (by 512KB):当我手动从空间内存中包含不可用的时,派生的堆大小为262656K (256.5MB),略大于最大堆大小-Xmx (512KB):

[heap size] = [eden space size] + [from space size] + [to space size] + [OldGen size]
  262656K   =       66048K      +       10752K      +     10752K      + 175104K

Why is the heap size slightly larger than the maximum heap size, -Xmx ?为什么堆大小略大于最大堆大小-Xmx

Firstly it seems strange, but as usual there is no magic.首先它看起来很奇怪,但像往常一样没有魔法。 The only way to find out the answer is to go deeper: to theopenjdk sources .找到答案的唯一方法是更深入:到openjdk 源代码 Just checkout and try to grep: grep -r "Xmx" .只需结帐并尝试 grep: grep -r "Xmx" .

There will be many tests, but they are not interesting to us.会有很多测试,但它们对我们来说并不有趣。 Two files can help us: /vm/runtime/arguments.cpp and /vm/memory/collectorPolicy.cpp两个文件可以帮助我们:/vm/runtime/arguments.cpp 和 /vm/memory/collectorPolicy.cpp

Let's take a look at arguments.cpp: there is only simple parameters parsing, eg for -Xmx256M it will be something like result = 256 * 1024 * 1024 (with some inlining from me).让我们看看arguments.cpp:只有简单的参数解析,例如对于-Xmx256M ,它将类似于result = 256 * 1024 * 1024 (我有一些内联)。 So there is no answer to our question.所以我们的问题没有答案。

But variable MaxHeapSize is initialized here, so we can try to find it.但是这里初始化了变量MaxHeapSize ,所以我们可以试着找到它。 The real usage of MaxHeapSize can be found in our /vm/memory/collectorPolicy.cpp (where Xmx is used only in comments), where all heap generation sizes are are chosen by some policy. MaxHeapSize的实际用法可以在我们的 /vm/memory/collectorPolicy.cpp(其中Xmx仅在注释中使用)中找到,其中所有堆生成大小都由某些策略选择。

If we skip all the details, we will find that all regions in heap should be aligned and region's sizes depends on ratio parameters.如果我们跳过所有细节,我们会发现堆中的所有区域都应该对齐,并且区域的大小取决于比率参数。

The default JVM parameters for generation ratios are -XX:NewRatio=2 (ratio of young gen:old gen sizes) and -XX:SurvivorRatio=8 (ratio of eden:survivor gens).生成比率的默认 JVM 参数是-XX:NewRatio=2 (年轻代:老一代大小的比例)和-XX:SurvivorRatio=8 (伊甸园:幸存者代的比例)。 But heap size is not always divisible by 3, 9 or something else, so there should be some rounding.但是堆大小并不总是能被 3、9 或其他东西整除,所以应该有一些四舍五入。 And, as I mentioned previously, there always should be some alignment.而且,正如我之前提到的,总是应该有一些对齐。

So, finally the answer : these sizes are the only possible sizes to satisfy the conditions of generational ratios and alignment.所以,最后的答案是:这些尺寸是满足世代比例和对齐条件的唯一可能尺寸。 It's just not always possible for sum of these values being equals to Xmx parameter value.这些值的总和并不总是可能等于Xmx参数值。

If you are interested in details, you can find the logic for rounding -XX:NewRatio here in method scale_by_NewRatio_aligned .如果您有兴趣的细节,你可以找到四舍五入逻辑-XX:NewRatio 这里的方法scale_by_NewRatio_aligned Try to find the same logic about -XX:SurvivorRatio yourself :)尝试自己找到关于-XX:SurvivorRatio的相同逻辑:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM