Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why class metaspace increase over time in Java 8?

I am running Tomcat application (which consists of Hibernate + EhCache). It is typical ORM webapp that shouldn't create lots of classes. However, Native Memory Tracking says "Nay".

During last 24 hours my webapp create on average ~1 new class per hour in Metaspace, with peak 11 classes per hour and peak 4 hours without creating classes.

So, I started with 7449 classes, after 24 hours I have 7481 classes. After inducing full GC, the number of classes can decrease by 0-2, but overall remains the same.

So

  1. If classes are loaded lazily, how can I estimate the total number of classes to be created in native memory?

    • I know Hibernate creates proxy classes. Is there a way to predict these proxy class count?
    • How to force JVM to load all classes eagerly?
  2. How can I log which new classes where loaded into Metaspace?

My configuration:

Server version: Apache Tomcat/8.0.28
OS Name:        Linux
OS Version:     3.14.34-27.48.amzn1.x86_64
Architecture:   amd64
JVM Version:    1.8.0_71-b15
JVM Vendor:     Oracle Corporation

JVM flags: -Xms512m -Xmx2G -XX:+UseG1GC -XX:NativeMemoryTracking=summary -XX:+UnlockDiagnosticVMOptions -XX:+PrintNMTStatistics
Hibernate 4.3.1
like image 430
danbst Avatar asked Jun 13 '26 14:06

danbst


1 Answers

Is there a way to predict these proxy class count

In general, no. An application in theory can emit class file bytecode at any time.

How to force JVM to load all classes eagerly?

You can't. Lazy class loading is one of the Java features. Well, you may scan the classpath and load all classes from all JARs, but that's not what you want to do, since it will end up with thousands of never-used classes.

How can I log which new classes where loaded into Metaspace?

Use -XX:+TraceClassLoading

like image 79
apangin Avatar answered Jun 15 '26 07:06

apangin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!