<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=hertzsprung" rel="nofollow">James Shaw</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-8229?redirect=false" rel="nofollow">SPR-8229</a></strong> and commented</p>
<p dir="auto">When an ApplicationContext is closed, any <code class="notranslate">@Configuration</code> class that was registered in that context does not get garbage collected.  If I understand the way class loading works, this is because the ClassLoader still holds a reference to the enhanced Configuration class.</p>
<p dir="auto">This is a problem for me because I want to create ApplicationContexts at runtime.</p>
<hr>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398109768" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/12556" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/12556/hovercard" href="https://github.com/spring-projects/spring-framework/issues/12556">#12556</a> ConfigurationClassEnhancer prevents BeanFactory from being GCd (<em><strong>"duplicates"</strong></em>)</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398109768" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/12556" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/12556/hovercard" href="https://github.com/spring-projects/spring-framework/issues/12556">#12556</a> ConfigurationClassEnhancer prevents BeanFactory from being GCd</li>
</ul>
<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=tbecker" rel="nofollow">Tommy Becker</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-7901?redirect=false" rel="nofollow">SPR-7901</a></strong> and commented</p>
<p dir="auto">We're experiencing OOM errors during unit test runs.  We make fairly heavy use of Java based context configuration in our application as well as the tests.  Upon investigation, I see that there are a number of ApplicationContext instances still referenced on the heap that should be collectable.  It appears that the way the ConfigurationClassEnhancer is registering its BeanMethodInterceptor with the CGLIB generated subclass is the problem.  In <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398096291" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/10601" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/10601/hovercard" href="https://github.com/spring-projects/spring-framework/issues/10601">#10601</a>, this was changed from Enhancer.registerCallbacks() to Enhancer.registerStaticCallbacks() to work around the fact that the former only applies the advice when method calls originate from the same thread that registered the callback.  However using the static callback mechanism results in a static reference from the generated subclass to the interceptor, which in turn keeps a reference to the BeanFactory.  So the BeanFactory is never eligible for GC, even after its been closed.  Although problems running unit tests reveled the problem, we're most likely experiencing it at runtime as well since our application does close contexts and start new ones dynamically in response to some events.</p>
<p dir="auto">As a potential fix, it would be nice if the BeanMethodInterceptor could receive a callback itself when the BeanFactory it references is closed, so it can then unregister itself from the CGLIB proxy.  Alternatively, perhaps a WeakReference to the BeanFactory could be used from the interceptor.</p>
<hr>
<p dir="auto"><strong>Affects:</strong> 3.0.3</p>
<p dir="auto"><strong>Attachments:</strong></p>
<ul dir="auto">
<li><a href="https://jira.spring.io/secure/attachment/17729/mylyn-context.zip" rel="nofollow">mylyn-context.zip</a> (<em>105.42 kB</em>)</li>
</ul>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398111696" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/12878" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/12878/hovercard" href="https://github.com/spring-projects/spring-framework/issues/12878">#12878</a> CGLIB enhanced <code class="notranslate">@Configuration</code> classes cannot be garbage collected (<em><strong>"is duplicated by"</strong></em>)</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398110499" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/12663" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/12663/hovercard" href="https://github.com/spring-projects/spring-framework/issues/12663">#12663</a> MemoryLeak in Cglib2AopProxy.ProxyCallbackFilter</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398111696" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/12878" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/12878/hovercard" href="https://github.com/spring-projects/spring-framework/issues/12878">#12878</a> CGLIB enhanced <code class="notranslate">@Configuration</code> classes cannot be garbage collected</li>
</ul>
<p dir="auto"><strong>Referenced from:</strong> commits <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/spring-projects/spring-framework/commit/d2faef44fa2c6f55d8a9fdac77ada63657a86864/hovercard" href="https://github.com/spring-projects/spring-framework/commit/d2faef44fa2c6f55d8a9fdac77ada63657a86864"><tt>d2faef4</tt></a>, <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/spring-projects/spring-framework/commit/43676bd660bcfdfea437fdb046ba05c6785d4121/hovercard" href="https://github.com/spring-projects/spring-framework/commit/43676bd660bcfdfea437fdb046ba05c6785d4121"><tt>43676bd</tt></a></p>
<p dir="auto">5 votes, 3 watchers</p>