简体   繁体   English

java G1 gc 占用太多内存

[英]java G1 gc takes too much of memory

For example, I set -Xmx as 40G.例如,我将 -Xmx 设置为 40G。 I expect my java processor won't use exceed 40G.我希望我的 java 处理器不会使用超过 40G。

My program is working fine with cms-gc.我的程序在 cms-gc 上运行良好。

But when I change to G1 gc with same memory(even 15% more memory).但是当我用相同的内存(甚至多出 15% 的内存)更改为 G1 gc 时。

It always killed by oom killer.它总是被oom杀手杀死。

I found some article like this: Why does my Java process consume more memory than Xmx?我发现了一些这样的文章: 为什么我的 Java 进程比 Xmx 消耗更多的内存?

It express:它表示:

 G1 is especially known for its 
 excessive appetite for additional memory, so be aware of this.

So I want to know, how to limit the memory that g1 gc used and why g1 use so much additional memory所以我想知道,如何限制 g1 gc 使用的内存以及为什么 g1 使用这么多额外的内存

The article you mention ( Why does my Java process consume more memory than Xmx? ) outlines it clearly.您提到的文章( 为什么我的 Java 进程比 Xmx 消耗更多内存? )清楚地概述了它。

The Java process requires memory for several things: Java 进程需要内存用于以下几件事:

  • Java heap (aka memory allocation pool) Java 堆(又名内存分配池)
  • Stack for each thread in your application应用程序中每个线程的堆栈
  • Memory used by the JVM itself (aka as permgen) JVM 本身使用的内存(又名 permgen)
  • Memory allocated by native functions (JRE or third-party libraries)本机函数(JRE 或第三方库)分配的内存

An additional problem is that the some JVM memory is not counted as permgen memory and cannot be controlled.另一个问题是一些JVM内存不计为永久内存,无法控制。

So if you want to restrict your Java application to 40 GB, you have to account for all types of memory.因此,如果要将 Java 应用程序限制为 40 GB,则必须考虑所有类型的内存。 Start with smaller values, like:从较小的值开始,例如:

-Xmx30g -XX:MaxPermSize=1g -Xss1m

Then observe the memory usage of your process and increase Xmx if the process safely stays away from the target 40GB.然后观察进程的内存使用情况,如果进程安全地远离目标 40GB,则增加Xmx

You can't limit what G1 needs to use.您无法限制G1需要使用的内容。 If you could - you would break everything or die with a heap out of memory error, because G1 would not have resources to properly function.如果可以的话 - 您会破坏所有内容或因堆内存不足错误而死亡,因为 G1 将没有资源来正常运行。 To explain why this algorithm needs to use extra memory, is not simple.要解释为什么这个算法需要使用额外的内存,并不简单。 It at least requires memory for space for card table and remembered sets , here is why .它至少需要用于card tableremembered sets空间的内存, 这就是原因 It requires memory for SATB queues, these are some data structures that "intercept" heap writes and reads when the GC cycle is active.它需要SATB队列的内存,这些是一些数据结构,当GC周期处于活动状态时“拦截”堆写入和读取。 And I bet there are more.我敢打赌还有更多。 In general, if you want concurrency (so that your application runs while GC cycle is active), you need to provide more memory.一般来说,如果您想要并发(以便您的应用程序在 GC 周期处于活动状态时运行),您需要提供更多内存。

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

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