spring-projects / spring-projects/spring-framework

Upgrade from 3.3.x to 3.4.x and 4.0.0: SpringBootTests throw OutOfMemoryError

Open
#34,279 12 comments 0 reactions 1 assignee View on GitHub

@sbrannen is already working on this.

Since Jun 30, 2026.

in: test status: waiting-for-triage
Dominant language
Java
Stars
60.2k
Forks
38.8k
Avg merge
5d 2h
Merged PRs (30d)
27

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() and shutdown() 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 and 4.0.0 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 from org.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 to immediate
  • Tried jetty and undertow instead of tomcat

Additional information:

  • We observe the same behaviour error with 3.4.2 and 4.0.0 (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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.