Skip to content

Suspected memory leak in SpringApplicationShutdownHook during spring boot testsuite execution #37129

@Mobe91

Description

@Mobe91

Spring Boot version: 3.1.2
Java version: 17.0.3, vendor: Eclipse Adoptium

Executing a spring-boot testsuite a java.lang.OutOfMemoryError is thrown after some time.

The testsuite is executed via maven-surefire-plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.1.2</version>
    <configuration>
        <forkCount>1</forkCount>
        <argLine>-Xmx2g -XX:+HeapDumpOnOutOfMemoryError --add-opens java.base/java.lang=ALL-UNNAMED</argLine>
    </configuration>
</plugin>

The test context caching has been limited to 1 by creating a spring.properties file with the following content:

spring.test.context.cache.maxSize=1

It has been verified that above setting takes effect by enabling DEBUG logging for org.springframework.test.context.cache.

From the above setup I would expect that only the last used application context is cached and all other application contexts become eligible for garbage collection.

However, when examining the heap dump that is created when the java.lang.OutOfMemoryError is thrown (compare -XX:+HeapDumpOnOutOfMemoryError setting above) I see that SpringApplicationShutdownHook#closedContexts contains 36 entries retaining around 1.6 GB of heap space. When analyzing the path to GC roots using Eclipse MAT for the application contexts contained in this Map then the only path to a GC root goes through the WeakHashMap$Entry holding the respective application context. When instructing Eclipse MAT to show paths to GC roots excluding weak references no paths are output.

Given that a full GC should happen before a java.lang.OutOfMemoryError is thrown it seems like something is blocking the GC of application contexts in SpringApplicationShutdownHook#closedContexts.

I can probably provide the heap dump if required (would need to check, though).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions