5

For example, I set -Xmx as 40G. I expect my java processor won't use exceed 40G.

My program is working fine with cms-gc.

But when I change to G1 gc with same memory(even 15% more memory).

It always killed by oom killer.

I found some article like this: Why does my Java process consume more memory than 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

yunfan
  • 760
  • 1
  • 11
  • 24

2 Answers2

2

The article you mention (Why does my Java process consume more memory than Xmx?) outlines it clearly.

The Java process requires memory for several things:

  • Java heap (aka memory allocation pool)
  • Stack for each thread in your application
  • Memory used by the JVM itself (aka as permgen)
  • Memory allocated by native functions (JRE or third-party libraries)

An additional problem is that the some JVM memory is not counted as permgen memory and cannot be controlled.

So if you want to restrict your Java application to 40 GB, you have to account for all types of memory. 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.

Codo
  • 75,595
  • 17
  • 168
  • 206
  • actually, I have specific all params your pointed for my program.And still killed by oom killer. But when I use cms gc. It works fine(but slow). So I want to know why. – yunfan Jul 02 '19 at 09:26
  • GC1 uses more memory than CMS not accounted for by the `Xmx` parameter. For GC1 you have to lower `Xmx` to achieve the same overall process size as for CMS. Once the process size is the same, OOM won't kill it anymore. OOM only cares about the process memory and doesn't know about the Java internal types of memory. – Codo Jul 02 '19 at 09:32
  • So I just want to know whether I can limit the G1 gc memory. I – yunfan Jul 02 '19 at 10:29
  • 1
    What is "gc memory"? – Codo Jul 02 '19 at 11:07
  • 2
    There is no Permgen in Java 8 or newer, so specifying `-XX:MaxPermSize=1g` for these versions will have no effect. – Holger Jul 02 '19 at 13:31
  • To the best of my knowledge, there is no parameter to control the GC1 overhead. There is no parameter either to control many other memory allocations of Java. But most likely the GC1 overhead is related to the pool allocation memory. So short story: You have to tune the `Xmx` parameter until the overall process memory fits your available memory. It will be trial and error as there is no parameter to direclty limit to overall process memory. – Codo Jul 02 '19 at 13:49
  • OK. So the conclusion is there is no way to limit g1 memory usage? – yunfan Jul 03 '19 at 02:30
0

You can't limit what G1 needs to use. 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. 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. It requires memory for SATB queues, these are some data structures that "intercept" heap writes and reads when the GC cycle is active. 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.

Eugene
  • 117,005
  • 15
  • 201
  • 306