Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-9851

Prevent memory leaks with @Configuration beans

    Details

    • Last commented by a User:
      true

      Description

      I have an app where I create many spring contexts for 3rd party classes, so they can autowire some resources, work in restricted environment, basically some "plugin system". These contexts are created and destroyed dynamically, sometimes in rapid pace.

      Since I added support for custom javaconfig style context configuration, app started to produce PermGen errors. I traced it to the root of the problem, shown in this simple test:

      package springctx;
       
      import org.springframework.context.annotation.AnnotationConfigApplicationContext;
       
      public class SpringCtxTest {
       
      	public static void main(String[] args) {
      		while (true) {
      			AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MyConfigClass.class);
      			ctx.destroy();
      		}
      	}
       
      }

      Config class:

      package springctx;
       
      import org.springframework.context.annotation.Configuration;
       
      @Configuration
      public class MyConfigClass {
       
      }

      When I run this, I can see clearly in VisualVM that PermGen is heading to the sky. Heapdump revealed gazillion of springctx.MyConfigClass$$EnhancerByCGLIB$$5d91109e entries in CachedIntrospectionResults

      When I run it without @Configuration annotation, everything is OK.

        Issue Links

          Activity

          Hide
          jmisur Juraj Misur added a comment -

          I forgot to mention: using cglib-nodep 2.2.2

          Show
          jmisur Juraj Misur added a comment - I forgot to mention: using cglib-nodep 2.2.2
          Hide
          juergen.hoeller Juergen Hoeller added a comment -

          Chris, could you please have a look at this? It's not the first CGLIB memory leak issue to come up this year, and we might have a better chance of dealing with it now that we upgraded to CGLIB 3.0...

          Show
          juergen.hoeller Juergen Hoeller added a comment - Chris, could you please have a look at this? It's not the first CGLIB memory leak issue to come up this year, and we might have a better chance of dealing with it now that we upgraded to CGLIB 3.0...
          Hide
          cbeams Chris Beams added a comment -

          Sure. Thanks, Juraj for the report.

          Show
          cbeams Chris Beams added a comment - Sure. Thanks, Juraj for the report.
          Hide
          cbeams Chris Beams added a comment -

          Juraj Misur, this is now fixed in the latest 3.2.0.BUILD-SNAPSHOT. Thanks again for the report!

          commit c8061393fbe3aa20bb51606c60d01a772d3df536
          Author: Phillip Webb <[email protected]>
          Commit: Chris Beams <[email protected]>
           
              Prevent memory leaks with @Configuration beans
              
              Refactor ConfigurationClassEnhancer to allow cglib caching of
              generated classes.  Prior to this commit each enhanced @Configuration
              class would consume permgen space when created.
              
              The CallbackFilter and Callback Types are now defined as static final
              members so that they can be shared by all enhancers.  Only the
              callbackInstances remain specific to a @Configuration class and
              these are not used by cglib as part of the cache key.
              
              Issue: SPR-9851

          Show
          cbeams Chris Beams added a comment - Juraj Misur , this is now fixed in the latest 3.2.0.BUILD-SNAPSHOT. Thanks again for the report! commit c8061393fbe3aa20bb51606c60d01a772d3df536 Author: Phillip Webb <[email protected]> Commit: Chris Beams <[email protected]>   Prevent memory leaks with @Configuration beans Refactor ConfigurationClassEnhancer to allow cglib caching of generated classes. Prior to this commit each enhanced @Configuration class would consume permgen space when created. The CallbackFilter and Callback Types are now defined as static final members so that they can be shared by all enhancers. Only the callbackInstances remain specific to a @Configuration class and these are not used by cglib as part of the cache key. Issue: SPR-9851
          Hide
          jmisur Juraj Misur added a comment -

          No PermGen errors in today's build 3.2.0.BUILD-20121016.200757-324. Thanks a lot!

          Show
          jmisur Juraj Misur added a comment - No PermGen errors in today's build 3.2.0.BUILD-20121016.200757-324. Thanks a lot!
          Hide
          cbeams Chris Beams added a comment - - edited

          Now backported to 3.1.x:

          commit ced6bb4569af81ea86850a4d028d11088488ef1e
          Author: Phillip Webb <[email protected]>
          Commit: Chris Beams <[email protected]>
           
              Prevent memory leaks with @Configuration beans
              
              Refactor ConfigurationClassEnhancer to allow cglib caching of
              generated classes.  Prior to this commit each enhanced @Configuration
              class would consume permgen space when created.
              
              The CallbackFilter and Callback Types are now defined as static final
              members so that they can be shared by all enhancers.  Only the
              callbackInstances remain specific to a @Configuration class and
              these are not used by cglib as part of the cache key.
              
              Issue: SPR-9851
              Backport-Commit: c8061393fbe3aa20bb51606c60d01a772d3df536

          Show
          cbeams Chris Beams added a comment - - edited Now backported to 3.1.x: commit ced6bb4569af81ea86850a4d028d11088488ef1e Author: Phillip Webb <[email protected]> Commit: Chris Beams <[email protected]>   Prevent memory leaks with @Configuration beans Refactor ConfigurationClassEnhancer to allow cglib caching of generated classes. Prior to this commit each enhanced @Configuration class would consume permgen space when created. The CallbackFilter and Callback Types are now defined as static final members so that they can be shared by all enhancers. Only the callbackInstances remain specific to a @Configuration class and these are not used by cglib as part of the cache key. Issue: SPR-9851 Backport-Commit: c8061393fbe3aa20bb51606c60d01a772d3df536

            People

            • Assignee:
              cbeams Chris Beams
              Reporter:
              jmisur Juraj Misur
              Last updater:
              Juergen Hoeller
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                5 years, 5 weeks, 2 days ago