Open
Description
- In our test setup code, we have a ContextConfiguration which adds beans to the application context (MockWebServer and GreenMail instances, which are shut down on ContextClosedEvent by calling their
stop()
andshutdown()
methods) - The SpringBootTests additionally use the following annotations:
@DirtiesContext(classMode = AFTER_EACH_TEST_METHOD)
and@TestInstance(PER_METHOD)
- With Spring Boot 3.3.5 the tests execute successfully
- With 3.4.1 and 3.4.2 the tests fail with an out of memory exception (
java.lang.OutOfMemoryError: Java heap space
) - When profiling the test runs, we observe that over 400 instances of
org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext
are referenced fromorg.springframework.beans.factory.support.DefaultListableBeanFactory
. It appears that this is about one instance per SpringBootTest. - With 3.3.5, the profiler (YourKit) marks them as
[Weak/Soft Reachable]
, whereas in 3.4.1 it doesn't (are they strong references now?) - We are not sure if there is a new leak, or if existed before and only now exhausts heap space
Example stacktrace:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xyz' defined in class path resource [abc.class]: Unexpected exception during bean creation
at app//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:536)
at app//org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:336)
at app//org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:289)
at app//org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:334)
at app//org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at app//org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:254)
at app//org.springframework.beans.factory.support.DefaultListableBeanFactory.addCandidateEntry(DefaultListableBeanFactory.java:1883)
at app//org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1847)
at app//org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveMultipleBeanCollection(DefaultListableBeanFactory.java:1737)
at app//org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveMultipleBeans(DefaultListableBeanFactory.java:1705)
at app//org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1580)
at app//org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1519)
at app//org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:913)
at app//org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791)
... 100 more
Caused by: java.lang.OutOfMemoryError: Java heap space
Environment:
- JDK Version: OpenJDK Runtime Environment Temurin-21.0.5+11 (build 21.0.5+11-LTS)
- Operating System: Windows 11
- Spring Boot Version: 3.4.1 (issue occurs), 3.3.5 (no issue)
- Build system: Gradle Wrapper 8.12
Attempted solutions:
- Set
server.shutdown
toimmediate
- Tried jetty and undertow instead of tomcat
Additional information:
- We observe the same behaviour error 3.4.2 (originally tested with 3.4.1)
- 3.3.x shows basically the same increase in heap memory usage over time like in this screenshot of our profiler. However, 3.3.x doesn't trigger the out of memory error.