A.R.K.S
A.R.K.S

Reputation: 1802

Guidelines to set MetaspaceSize - java 8

What is the default value of MetaspaceSize for 64-bit servers? I couldn't find it in the official documentation.

I'm observing that in a server JVM process, at times, the GC frequency becomes high and keeps growing. If I restart the service a few times, it returns to stable. I think its due to the JRE upgrade.

JVM Heap max size is set to be 6GB but when this problem occurs, we see only 3GB heap being used. Metaspace grows by very little and is almost always full. I tried increasing the metaspace to 1GB and it improves the throughput.

I think what is happening is Metaspace by default is set to a very low value and thus GC kicks in. High water mark is continuously increased(again by a very low amount) every time a GC occurs.

I want to set the MetaspaceSize (not sure what is the current value).

Oracle docs say there are no guidelines to know what to set MetaspaceSize to. But is there a way to find out what would be the correct value to set it to?

One hint I got from Oracle docs is this:

If the committed space available for class metadata as a percentage of the total committed space for class metadata is greater than MaxMetaspaceFreeRatio, then the high-water mark will be lowered. If it is less than MinMetaspaceFreeRatio, then the high-water mark will be raised.

But still not able to figure out how to stabilize the GCs..I have three questions:

  1. What is the default MetaspaceSize on 64 bit servers?
  2. What are the default ratios: MaxMetaspaceFreeRatio, MinMetaspaceFreeRatio set to? Answer: It shows Min is 40, Max is 70
  3. How to go about deciding oon a Metaspacesize value?

Upvotes: 19

Views: 62778

Answers (3)

mrts
mrts

Reputation: 18935

Since Java 8, the two most important metaspace settings can be controlled with the following command-line options:

  • -XX:MetaspaceSize=N - when the space initially committed for class metadata reaches this level, a garbage collection is induced; the level may be subsequently raised or lowered depending on collection results,
  • -XX:MaxMetaspaceSize=N - sets the maximum size of the metaspace.

Quoting Oracle HotSpot VM Garbage Collection Tuning Guide,

  • "Class metadata is deallocated when the corresponding Java class is unloaded. Java classes are unloaded as a result of garbage collection, and garbage collections may be induced to unload classes and deallocate class metadata. When the space committed for class metadata reaches a certain level (a high-water mark), a garbage collection is induced. After the garbage collection, the high-water mark may be raised or lowered depending on the amount of space freed from class metadata. The high-water mark would be raised so as not to induce another garbage collection too soon. The high-water mark is initially set to the value of the command-line option -XX:MetaspaceSize. [...] The default size of -XX:MetaspaceSize is platform-dependent and ranges from 12 MB to about 20 MB."
  • "The amount of native memory that can be used for class metadata is by default unlimited. Use the option -XX:MaxMetaspaceSize to put an upper limit on the amount of native memory used for class metadata."

The WildFly application server sets the metaspace options in standalone.conf to 96 MB / 256 MB respectively as follows:

... -XX:MetaspaceSize=96M -XX:MaxMetaspaceSize=256m ...

However, MaxMetaspaceSize might need to be much larger in large production applications as it needs to contain metadata for all loaded classes in your application and you might want to specify a higher value for MetaspaceSize to avoid early garbage collections.

See also @Eugene's answer for instructions how to empirically determine metaspace settings.

Upvotes: 7

Eugene
Eugene

Reputation: 120868

MetaspaceSize is the value when a Full GC will be triggered, its not initial space nor maximum space.

The default value for it is 20MB (at least on jdk-8 to jdk-13):

java -XX:+PrintFlagsFinal -version | grep MetaspaceSize

will produce:

size_t MetaspaceSize = 21807104 // 20MB

This means that when Metaspace reaches 20MB a Full GC will be triggered and if you can avoid it, that would be good.

Considering how much data metaspace can potentially hold (methods, annotations, etc), it seems to me that 20MB is a very small value to start with. Unfortunately, choosing a correct value is not easy.

You have two options, enable gc logging -Xlog:gc and let the app run for some time and then find instances like:

[Full GC (Metadata GC Threshold) ...]

And understand from these what you need to set and how.

Or start the process with:

java -XX:NativeMemoryTracking=detail

connect to the PID via :

jcmd <YourJVMPID> VM.metaspace

And find lines like these:

 Virtual space:
    Non-class space:        8.00 MB reserved,       4.25 MB ( 53%) committed
    Class space:            1.00 GB reserved,     512.00 KB ( <1%) committed
    Both:                   1.01 GB reserved,       4.75 MB ( <1%) committed

And get the value from Non-class space after the app has been running for at least a day IMHO.

There are various other settings on how to control Metaspace, which you can find out like this:

java -XX:+PrintFlagsFinal -version | grep Metaspace

Upvotes: 18

Adam
Adam

Reputation: 236

I think the most interesting question is #3 so I'll answer that one first. The way I approached it was to run my application for 24 hours with gc logging turned on using these settings:

-XX:+PrintGC
-XX:+PrintGCDetails
-XX:+PrintGCDateStamps
-XX:+PrintGCCause
-XX:+PrintTenuringDistribution
-XX:+PrintGCApplicationStoppedTime
-XX:+PrintGCApplicationConcurrentTime
-Xloggc:gc.log

Then I did this:

grep Metaspace gc.log

and saw that the "used" value quickly grew to 80M ... so I set MetaspaceSize to 100M

-XX:MetaspaceSize=100M

and voila - no more gcs due to metaspace.

It looks like @Pillar's comment pretty much answered your first 2 questions on how to determine the default Metaspace settings for your implementation ... I'll include it here for completeness:

java -XX:+UnlockDiagnosticVMOptions -XX:+PrintFlagsFinal -version -XX:+UseG1GC | grep Metaspace

Technically, @Pillar's comment doesn't tell you the default Metaspace size for any 64 bit server, but but that's because your first question is a bit misleading, since according to the documentation these defaults vary per implementation.

See:

https://docs.oracle.com/javase/8/docs/technotes/guides/vm/gctuning/considerations.html

Specifically, the part at the bottom where it says, "The default size of MetaspaceSize is platform-dependent and ranges from 12 MB to about 20 MB"

Upvotes: 10

Related Questions