40

In an application I have the following -verbose:gc

[GC (Metadata GC Threshold)  8530310K->2065630K(31574016K), 0.3831399 secs]
[Full GC (Metadata GC Threshold)  2065630K->2053217K(31574016K), 3.5927870 secs]
[GC (Metadata GC Threshold)  8061486K->2076192K(31574016K), 0.0096316 secs]
[Full GC (Metadata GC Threshold)  2076192K->2055722K(31574016K), 0.9376524 secs]
[GC (Metadata GC Threshold)  8765230K->2100440K(31574016K), 0.0150190 secs]
[Full GC (Metadata GC Threshold)  2100440K->2077052K(31574016K), 4.1662779 secs]

What is this "Metadata GC threshold" and how to I reduce it. Note: while the Full GC spends a long time cleaning up, it does not actually clean up much, i.e. it would be better if it didn't do this.

Ilya Kurnosov
  • 3,180
  • 3
  • 23
  • 37
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130

2 Answers2

52

The log message tells that GC was caused by Metaspace allocation failure. Metaspaces hold class metadata. They have appeared in Java 8 to replace PermGen.

Here are some options to tune Metaspaces.
You may want to set one or several of the following options:

-XX:MetaspaceSize=100M Sets the size of the allocated class metadata space that will trigger a garbage collection the first time it is exceeded;

-XX:InitialBootClassLoaderMetaspaceSize=32M to increase the boot class loader Metaspace;

-XX:MinMetaspaceFreeRatio=50 to make Metaspaces grow more agressively;

-XX:MaxMetaspaceFreeRatio=80 to reduce the chance of Metaspaces shrinking;

-XX:MinMetaspaceExpansion=4M the minumum size by which a Metaspace is exanded;

-XX:MaxMetaspaceExpansion=16M the maximum size to expand a Metaspace by without Full GC.

Eugene
  • 117,005
  • 15
  • 201
  • 306
apangin
  • 92,924
  • 10
  • 193
  • 247
  • 4
    Metasapce in JDK8 is set to 21MB by default on 64-bit VM. For high performance applications, you may want to consider setting min,init to max (as one would with PermGen in JDK7) and constraint the high water mark sizing ratios Metaspace Full GC's are expensive as they run a Old Heap Full GC as well and hence and increase to total app p95, p99 runTime latency from elevated GC pause times. -XX:MetaspaceSize=1024m -XX:MaxMetaspaceSize=1024m -XX:MinMetaspaceFreeRatio=0 -XX:MaxMetaspaceFreeRatio=100 – ak_2050 Feb 25 '15 at 22:48
  • 1
    It seems that most of these options aren't documented in the man page for java8 (http://docs.oracle.com/javase/8/docs/technotes/tools/unix/java.html) , and that article you posted where you got these is from 2014, were these removed or are they just undocumented? – mgrandi Sep 10 '16 at 00:50
  • 1
    @mgrandi These flags were never listed on that page. Most VM flags are not well documented rather than in source code or in blog posts. – apangin Sep 10 '16 at 08:56
  • @Eugene The answer was written long before [JDK-8151845](https://bugs.openjdk.java.net/browse/JDK-8151845). Feel free to edit if you wish. – apangin Feb 06 '18 at 21:37
20

While there is already an accepted answer, wanted to mention that there is also :

-XX:MaxMetaspaceSize=<NNN> where <NNN> is the maximum amount of space to be allocated for class metadata (in bytes).

Also from here,

Garbage collection of the dead classes and classloaders is triggered once the class metadata usage reaches the “MaxMetaspaceSize”.

There is a list of available options in this post.

randers
  • 5,031
  • 5
  • 37
  • 64
Ravindra HV
  • 2,558
  • 1
  • 17
  • 26