New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
spring-issuemaster opened this Issue Oct 26, 2010 · 3 comments

Comments

Projects
None yet
2 participants
@spring-issuemaster
Copy link
Collaborator

spring-issuemaster commented Oct 26, 2010

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:

  • #10532 Allow for concurrent test execution in the TestContext framework
  • #16200 Introduce system property to disable context caching in the TestContext framework
  • #12710 Limit size of context cache in the TestContext framework ("is superseded by")

Referenced from: commits 0cb22fc, d66d160

2 votes, 4 watchers

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator

spring-issuemaster commented Apr 18, 2015

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.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator

spring-issuemaster commented Apr 19, 2015

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.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator

spring-issuemaster commented Apr 19, 2015

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment