quarkusio / quarkus

Quarkus: Supersonic Subatomic Java.
https://quarkus.io
Apache License 2.0
13.73k stars 2.67k forks source link

@TestProfile do not 'release' Metaspace on each profile run #33990

Closed gkozyryatskyy closed 1 year ago

gkozyryatskyy commented 1 year ago

Describe the bug

When you run few tests with @TestProfile and different profiles in a row, jvm Metaspace consumption incrementally increased.

Expected behavior

Next (after first) @TestProfile executions not lead to MEM consumption increase.

Actual behavior

Currently when you run few tests with @TestProfile and different profiles in a row, jvm Metaspace consumption incrementally increased. Log example from my test:

total:[377]mb free:[203]mb max:[800]mb, meta:[93]mb
total:[587]mb free:[321]mb max:[800]mb, meta:[174]mb
total:[662]mb free:[305]mb max:[800]mb, meta:[252]mb
total:[776]mb free:[180]mb max:[800]mb, meta:[329]mb
total:[776]mb free:[302]mb max:[800]mb, meta:[337]mb
total:[800]mb free:[253]mb max:[800]mb, meta:[407]mb
total:[800]mb free:[254]mb max:[800]mb, meta:[429]mb
total:[800]mb free:[180]mb max:[800]mb, meta:[499]mb
total:[800]mb free:[79]mb max:[800]mb, meta:[575]mb
total:[800]mb free:[54]mb max:[800]mb, meta:[648]mb

Where each line is from separate @TestProfile execution

How to Reproduce?

Reproducer can be 'unclear', but i created some https://github.com/gkozyryatskyy/quarkus-test-mem-leak-reproducer so you can understand the problem. I add some random dependency and classes to the reproducer, for app to @Inject at leas something. I assume, as more Injection we have, as more Metaspace is consumed for each run. But you are interested in tests execution https://github.com/gkozyryatskyy/quarkus-test-mem-leak-reproducer/tree/main/src/test/java

In my 'real' project tests, Metaspace consumption is worse.

Output of uname -a or ver

macOs 13.3.1 (a)

Output of java -version

openjdk version "19.0.1" 2022-10-18

GraalVM version (if different from Java)

No response

Quarkus version or git rev

3.1.0.Final

Build tool (ie. output of mvnw --version or gradlew --version)

Apache Maven 3.9.0 (9b58d2bad23a66be161c4664ef21ce219c2c8584)

Additional information

No response

gsmet commented 1 year ago

/cc @geoand

geoand commented 1 year ago

I don't see any problems.

If you actually size the Metaspace properly to provide headroom to load all the classes (I used -XX:MaxMetaspaceSize=390m), the JVM will properly GC the unused classes (i.e. application classes that are loaded by no longer used Quarkus ClassLoaders).

Here is a Graph a got from YourKit that shows GC of Metaspace in action:

Screenshot from 2023-06-13 13-43-53

gkozyryatskyy commented 1 year ago

@geoand Thank you for your reply!

The screen, you provided is from my reproducer? What you see in the logs? I see next picture:

===================Start: total:[267]mb free:[131]mb max:[1024]mb meta:[79]mb===================
===================End: total:[941]mb free:[5]mb max:[1024]mb meta:[79]mb===================
===================Start: total:[514]mb free:[249]mb max:[1024]mb meta:[137]mb===================
===================End: total:[1024]mb free:[47]mb max:[1024]mb meta:[137]mb===================
===================Start: total:[890]mb free:[613]mb max:[1024]mb meta:[194]mb===================
===================End: total:[1024]mb free:[58]mb max:[1024]mb meta:[194]mb===================
===================Start: total:[437]mb free:[241]mb max:[1024]mb meta:[196]mb===================
===================End: total:[985]mb free:[8]mb max:[1024]mb meta:[196]mb===================
===================Start: total:[907]mb free:[502]mb max:[1024]mb meta:[253]mb===================
===================End: total:[1024]mb free:[13]mb max:[1024]mb meta:[253]mb===================
===================Start: total:[607]mb free:[427]mb max:[1024]mb meta:[196]mb===================
===================End: total:[978]mb free:[8]mb max:[1024]mb meta:[196]mb===================
===================Start: total:[874]mb free:[469]mb max:[1024]mb meta:[253]mb===================
===================End: total:[1024]mb free:[14]mb max:[1024]mb meta:[253]mb===================

and it shows that metaspace is growing.

I also will run it with -XX:MaxMetaspaceSize, as you suggested, but lets set enough Metaspace for few profile runs, but not to all of them, because all tests are equivalent and should not increate Metaspace consumption after first run

Running with mvn install -DargLine='-Xmx1024m -XX:MaxMetaspaceSize=200m'

geoand commented 1 year ago

The screen, you provided is from my reproducer?

Yes, your sampple with double the number profiles / tests

gkozyryatskyy commented 1 year ago

@geoand Sorry, I not finished previous comment (published unfinished). Now I updated it with -XX:MaxMetaspaceSize run. Please check.

geoand commented 1 year ago

That's why I said the metaspace needs to be sized properly.

gkozyryatskyy commented 1 year ago

@geoand but this is exactly the problem, if we have enough metaspce for a single profile run, we should be able to run any number of profiles sequentially with the same metaspace limit.

As i mentioned, in my 'real' project metaspace consumption is worse. It is starting from meta:[93]mb and ends up with meta:[648]mb. So you are saying this is ok and i just need to give more and more MEM when Im adding more profiles?

geoand commented 1 year ago

but this is exactly the problem, if we have enough metaspce for a single profile run

That's not what I said, I said we need enough to get things running. Obviously you should not be required to have the entire Metaspace

geoand commented 1 year ago

So you are saying this is ok and i just need to give more and more MEM when Im adding more profiles?

What I am saying is that I took your sample, and whether or not I doubled the profiles, I still needed around 300MB of metaspace to run it. The doubling of the profile was only done to introduce more GCs, which actually happened as expected.

gkozyryatskyy commented 1 year ago

@geoand Got your point. But this is strange that we can run fist profile and cant run others.

In some time I will try to reproduce the problem with -XX:MaxMetaspaceSize=390m, will let you know, if I will found anything.

Thank you for your help!

geoand commented 1 year ago

🙏🏼