yunfan
yunfan

Reputation: 800

java G1 gc takes too much of memory

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

Upvotes: 5

Views: 4627

Answers (2)

Eugene
Eugene

Reputation: 120858

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.

Upvotes: 0

Codo
Codo

Reputation: 78835

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.

Upvotes: 2

Related Questions