Skip to content

Commit

Permalink
Merge pull request quarkusio#41723 from gsmet/clear-ResourceManager-c…
Browse files Browse the repository at this point in the history
…ache

Clear com.sun.naming.internal.ResourceManager#propertiesCache
  • Loading branch information
gsmet authored Jul 9, 2024
2 parents 014bb15 + 38d9997 commit 224fb37
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
3 changes: 2 additions & 1 deletion build-parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,8 @@
</systemPropertyVariables>
<!-- limit the amount of memory surefire can use, 1500m should be plenty-->
<!-- set tmpdir as early as possible because surefire sets it too late for JDK16 -->
<argLine>${jacoco.agent.argLine} -Xmx1500m -XX:MaxMetaspaceSize=1500m -Djava.io.tmpdir="${project.build.directory}" ${surefire.argLine.additional}</argLine>
<!-- the add-opens is here to allow to clear the propertiesCache in com.sun.naming.internal.ResourceManager -->
<argLine>${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</argLine>
<excludedEnvironmentVariables>MAVEN_OPTS</excludedEnvironmentVariables>
</configuration>
</plugin>
Expand Down
10 changes: 3 additions & 7 deletions extensions/jaxb/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

<artifactId>quarkus-jaxb-deployment</artifactId>
<name>Quarkus - JAXB - Deployment</name>
<properties>
<surefire.argLine.additional>-Duser.language=en</surefire.argLine.additional>
</properties>

<dependencies>
<dependency>
Expand Down Expand Up @@ -52,13 +55,6 @@
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Duser.language=en</argLine>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,46 @@ 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.
* <p>
* 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);
}
}

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);
}
}
}

0 comments on commit 224fb37

Please sign in to comment.