Fix memory leak regression involving @Async methods
Spring Framework 5.2 M1 introduced a memory leak for applications using @Async methods. Specifically, in a large test suite with multiple ApplicationContexts that were closed (e.g., via @DirtiesContext, @MockBean, or context cache eviction), the JVM process could run out of memory. Underlying cause: Due to a missing equals() implementation in Spring's new AnnotationCandidateClassFilter, CGLIB's static cache of generated classes indirectly retained references to BeanFactory instances for the closed ApplicationContexts for the duration of the test suite. This commit fixes this regression by introducing a proper equals() implementation in AnnotationCandidateClassFilter. This commit also introduces corresponding hashCode() and toString() implementations. Closes gh-23571
This commit is contained in:
@@ -31,6 +31,7 @@ import org.springframework.util.Assert;
|
||||
* {@link #forMethodAnnotation method}.
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
* @author Sam Brannen
|
||||
* @since 2.0
|
||||
* @see AnnotationClassFilter
|
||||
* @see AnnotationMethodMatcher
|
||||
@@ -63,7 +64,7 @@ public class AnnotationMatchingPointcut implements Pointcut {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new AnnotationMatchingPointcut for the given annotation type.
|
||||
* Create a new AnnotationMatchingPointcut for the given annotation types.
|
||||
* @param classAnnotationType the annotation type to look for at the class level
|
||||
* (can be {@code null})
|
||||
* @param methodAnnotationType the annotation type to look for at the method level
|
||||
@@ -76,7 +77,7 @@ public class AnnotationMatchingPointcut implements Pointcut {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new AnnotationMatchingPointcut for the given annotation type.
|
||||
* Create a new AnnotationMatchingPointcut for the given annotation types.
|
||||
* @param classAnnotationType the annotation type to look for at the class level
|
||||
* (can be {@code null})
|
||||
* @param methodAnnotationType the annotation type to look for at the method level
|
||||
@@ -139,10 +140,9 @@ public class AnnotationMatchingPointcut implements Pointcut {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "AnnotationMatchingPointcut: " + this.classFilter + ", " +this.methodMatcher;
|
||||
return "AnnotationMatchingPointcut: " + this.classFilter + ", " + this.methodMatcher;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Factory method for an AnnotationMatchingPointcut that matches
|
||||
* for the specified annotation at the class level.
|
||||
@@ -169,12 +169,13 @@ public class AnnotationMatchingPointcut implements Pointcut {
|
||||
/**
|
||||
* {@link ClassFilter} that delegates to {@link AnnotationUtils#isCandidateClass}
|
||||
* for filtering classes whose methods are not worth searching to begin with.
|
||||
* @since 5.2
|
||||
*/
|
||||
private static class AnnotationCandidateClassFilter implements ClassFilter {
|
||||
|
||||
private final Class<? extends Annotation> annotationType;
|
||||
|
||||
public AnnotationCandidateClassFilter(Class<? extends Annotation> annotationType) {
|
||||
AnnotationCandidateClassFilter(Class<? extends Annotation> annotationType) {
|
||||
this.annotationType = annotationType;
|
||||
}
|
||||
|
||||
@@ -182,6 +183,29 @@ public class AnnotationMatchingPointcut implements Pointcut {
|
||||
public boolean matches(Class<?> clazz) {
|
||||
return AnnotationUtils.isCandidateClass(clazz, this.annotationType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (!(obj instanceof AnnotationCandidateClassFilter)) {
|
||||
return false;
|
||||
}
|
||||
AnnotationCandidateClassFilter that = (AnnotationCandidateClassFilter) obj;
|
||||
return this.annotationType.equals(that.annotationType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.annotationType.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getClass().getName() + ": " + this.annotationType;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user