Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8: Why does Metaspace size increase but number of loaded classes stay the same?

Tags:

In our app running on Jdk 8 we use VisualVM to track the usage of loaded classes and the usage of the metaspace.

At some point in time while our app is running we see that the number of loaded classes don't increase any more but the metaspace still increases in it's size while our program is running. So what else apart from classes is stored in metaspace, that could cause that?

like image 995
longliveenduro Avatar asked Jun 19 '15 12:06

longliveenduro


People also ask

Why does Metaspace increase?

Metaspace is a native memory region that stores metadata for classes. As a class is loaded by the JVM, its metadata (i.e. its runtime representation in the JVM) is allocated into the Metaspace. The Metaspace occupancy grows as more and more classes are loaded.

How does Metaspace work in Java?

In the place of PermGen, a new feature called Meta Space has been introduced. MetaSpace grows automatically by default. Here, the garbage collection is automatically triggered when the class metadata usage reaches its maximum metaspace size. It is removed from java 8.

What is Max Metaspace size?

The default size of -XX:MetaspaceSize is platform-dependent and ranges from 12 MB to about 20 MB.

What is Metaspace in Java 8 How does it differ from PermGen space?

Simply put, Metaspace is a new memory space – starting from the Java 8 version; it has replaced the older PermGen memory space. The most significant difference is how it handles memory allocation. Specifically, this native memory region grows automatically by default.


1 Answers

While your program is running, some parts of your code may be determined as "hot" by HotSpot's JIT compiler. This will cause those parts to be transformed/compiled to native code, and also some other code may be inlined into it. This native code representation has to go somewhere, and it goes into the same place as other class metadata - the Metaspace.

It explains continuous growth you're seeing: hot parts are determined over time using a simple metric of how much times did that piece of code got executed. Over time more and more code pieces will be JIT'ed as they'll hit threshold set by -XX:CompileThreshold (defaults to 10000)

like image 116
Vyacheslav Tverskoy Avatar answered Sep 24 '22 10:09

Vyacheslav Tverskoy