Skip to content

Use soft or weak references for context caching in the TestContext framework [SPR-7687] #12343

Closed

Description

@spring-projects-issues

Eric Sirianni opened SPR-7687 and commented

We are using the Spring TestContext framework (SpringJUnit4ClassRunner) for JUnit test execution. We are hitting OutOfMemoryErrors when running a large number of tests in batch. The problem is that the ApplicationContext objects are building up in the ContextCache (org.springframework.test.context.ContextCache), consuming a large amount of memory.

We have worked around the problem by annotating @DirtiesContext on each JUnit test class.

However, it seems that this issue could be avoided if ContextCache wrapped its cached ApplicationContexts in WeakReferences. This seems to be a best practice when caching potentially large objects such as these.


Affects: 3.0.3

Issue Links:

Referenced from: commits 0cb22fc, d66d160

2 votes, 4 watchers

Activity

  1. spring-projects-issues commented on Apr 18, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    Completed as described in GitHub commit 0cb22fc:

    Use SoftReferences for context caching in the TCF

    Prior to this commit, the ContextCache in the Spring TestContext Framework (TCF) cached ApplicationContexts in a ConcurrentHashMap using strong references. This practice can occasionally lead to OutOfMemoryErrors when running a large number of tests in a test suite with varying context configuration since the context cache becomes overpopulated over time.

    This commit addresses this issue by using Spring's ConcurrentReferenceHashMap which uses SoftReferences for both the keys (i.e., MergedContextConfiguration instances) and values (i.e., ApplicationContexts) stored in the map that backs the ContextCache.

  2. spring-projects-issues commented on Apr 19, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    Reverted previous changes as explained in GitHub commit d66d160:

    Use ConcurrentHashMaps in DefaultContextCache

    The changes made in 0cb22fc would result in contexts not being properly closed if evicted from the ConcurrentReferenceHashMap by the Garbage Collector.

    This commit reverts those changes and returns to using standard ConcurrentHashMaps for the time being.

  3. spring-projects-issues commented on Apr 19, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    Resolving this issue as "Won't Fix" since the use of the soft or weak references would prevent the TestContext framework from properly closing application contexts that get transparently evicted by the garbage collector.

    An an alternative, we will instead have to investigate a more robust solution like the one proposed in #12710.

  4. added
    status: declined
    in: test
    type: enhancement
    on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: teststatus: declinedtype: enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions