Skip to content

ConfigurationClassEnhancer prevents BeanFactory from being GCd [SPR-7901] #12556

Closed
@spring-projects-issues

Description

@spring-projects-issues

Tommy Becker opened SPR-7901 and commented

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 #10601, 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.

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.


Affects: 3.0.3

Attachments:

Issue Links:

Referenced from: commits d2faef4, 43676bd

5 votes, 3 watchers

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: coreIssues in core modules (aop, beans, core, context, expression)type: bugA general bug

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions