From 38d999744f3802721d9d9b03cf3a2b176e58954d Mon Sep 17 00:00:00 2001 From: Guillaume Smet Date: Sat, 6 Jul 2024 11:25:27 +0200 Subject: [PATCH] Clear com.sun.naming.internal.ResourceManager#propertiesCache This is only effective if the class is made accessible, which we make in our code. I noticed this a few times in my heap dumps and I noticed this was also handled in some other frameworks (until the switch to Java 9 that made the --add-opens mandatory). I think dealing with it in our codebase is a good idea from a sustainability point of view. --- build-parent/pom.xml | 3 +- extensions/jaxb/deployment/pom.xml | 10 ++---- .../main/java/io/quarkus/test/ClearCache.java | 34 +++++++++++++++---- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/build-parent/pom.xml b/build-parent/pom.xml index d9706721a99be..62b4681b16f1d 100644 --- a/build-parent/pom.xml +++ b/build-parent/pom.xml @@ -440,7 +440,8 @@ - ${jacoco.agent.argLine} -Xmx1500m -XX:MaxMetaspaceSize=1500m -Djava.io.tmpdir="${project.build.directory}" ${surefire.argLine.additional} + + ${jacoco.agent.argLine} -Xmx1500m -XX:MaxMetaspaceSize=1500m -Djava.io.tmpdir="${project.build.directory}" ${surefire.argLine.additional} --add-opens java.naming/com.sun.naming.internal=ALL-UNNAMED MAVEN_OPTS diff --git a/extensions/jaxb/deployment/pom.xml b/extensions/jaxb/deployment/pom.xml index a496418bac757..e4cf5cd173d01 100644 --- a/extensions/jaxb/deployment/pom.xml +++ b/extensions/jaxb/deployment/pom.xml @@ -11,6 +11,9 @@ quarkus-jaxb-deployment Quarkus - JAXB - Deployment + + -Duser.language=en + @@ -52,13 +55,6 @@ - - org.apache.maven.plugins - maven-surefire-plugin - - -Duser.language=en - - diff --git a/test-framework/junit5-internal/src/main/java/io/quarkus/test/ClearCache.java b/test-framework/junit5-internal/src/main/java/io/quarkus/test/ClearCache.java index 409b5e930b9f5..179ef1d5e5295 100644 --- a/test-framework/junit5-internal/src/main/java/io/quarkus/test/ClearCache.java +++ b/test-framework/junit5-internal/src/main/java/io/quarkus/test/ClearCache.java @@ -13,17 +13,26 @@ public class ClearCache { public static void clearCaches() { clearAnnotationCache(); + clearResourceManagerPropertiesCache(); clearBeansIntrospectorCache(); } private static void clearAnnotationCache() { + clearMap(AnnotationUtils.class, "repeatableAnnotationContainerCache"); + } + + /** + * This will only be effective if the tests are launched with --add-opens java.naming/com.sun.naming.internal=ALL-UNNAMED, + * which is the case in the Quarkus codebase where memory usage is actually an issue. + *

+ * While not mandatory, this actually helps so enabling it only in the Quarkus codebase has actual value. + */ + private static void clearResourceManagerPropertiesCache() { try { - Field f = AnnotationUtils.class.getDeclaredField("repeatableAnnotationContainerCache"); - f.setAccessible(true); - ((Map) (f.get(null))).clear(); - } catch (NoSuchFieldException | IllegalAccessException e) { - //ignore - log.debug("Failed to clear annotation cache", e); + clearMap(Class.forName("com.sun.naming.internal.ResourceManager"), "propertiesCache"); + } catch (ClassNotFoundException e) { + // ignore + log.debug("Unable to load com.sun.naming.internal.ResourceManager", e); } } @@ -31,8 +40,19 @@ private static void clearBeansIntrospectorCache() { try { Introspector.flushCaches(); } catch (Exception e) { - //ignore + // ignore log.debug("Failed to clear java.beans.Introspector cache", e); } } + + private static void clearMap(Class clazz, String mapField) { + try { + Field f = clazz.getDeclaredField(mapField); + f.setAccessible(true); + ((Map) (f.get(null))).clear(); + } catch (Exception e) { + // ignore + log.debugf(e, "Failed to clear cache for %s#%s cache", clazz.getName(), mapField); + } + } }