You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Use soft or weak references for context caching in the TestContext framework [SPR-7687] #12343
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.
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.
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.
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.
Activity
spring-projects-issues commentedon Apr 18, 2015
Sam Brannen commented
Completed as described in GitHub commit 0cb22fc:
spring-projects-issues commentedon Apr 19, 2015
Sam Brannen commented
Reverted previous changes as explained in GitHub commit d66d160:
spring-projects-issues commentedon 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.