Sylvère Richard opened SPR-17435 and commented

+How to reproduce:+

create several unit tests such as this one:

@RunWith(SpringRunner.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class MyTest {
  @Autowired
  MyCGLibProxiedService myService;

  @Test
  public void test1() {
    // test some stuff
  }
  @Test
  public void test2() {
     // test some stuff
  }
}

Because of the DirtiesContext, the application context test is closed and recreated after each test.

+Expected Behaviour:+

When running several unit tests such as the one above, the total memory needed to run the test suite should not increase as we add more tests.

In particular, when an application context is closed, the field generatedClasses (LoadingCache) of the class AbstractClassGenerator.ClassLoaderData should be cleared for that application context.

+Current Behaviour:+

The more unit tests we have, the more memory is required to run the test suite.

Indeed, the field generatedClasses caches instances of CglibAopProxy.ProxyCallbackFilter.

CglibAopProxy.ProxyCallbackFilter instance contains a reference to AdvisedSupport instance.

AdvisedSupport instance contains a reference to a closed AnnotationConfigApplicationContext.

Since all these references are strong one, it means that all closed instances of AnnotationConfigApplicationContext are never collected by the GC.

!image-2018-10-25-23-24-44-279.png!

+Possible Solutions:+ * we could add a way to disable this cache when running unit tests. * we could listen to ContextClosedEvent and clear entries in the cache that reference a closed application context. * ...


Affects: 5.0.10

Attachments: - image-2018-10-25-23-24-44-279.png (101.34 kB)

Comment From: snicoll

There has been some improvements in that area lately so I am going to close this in favor of #26266. If it turns out that there is still a leak, we can reopen.