Skip to content

Scoped-proxy memory leak w/ @Resource injection [SPR-9176] #13814

Closed

Description

@spring-projects-issues

Leigh Anderson opened SPR-9176 and commented

New entries are added to dependentBeanMap and dependenciesForBean map every time that a new instance of the request-scoped bean is created. This is because the proxy object representing the HttpServletRequest has a bean name like "$Proxy10@XXXXX" where XXXXX changes for each request. These entries are never removed, and eventually fill up the old generation.

Attached is a web application which demonstrates the issue. Visit "http://localhost:8080/leak" repeatedly and watch the count increase.


Affects: 3.0.5

Attachments:

Sub-tasks:

Issue Links:

3 votes, 4 watchers

Activity

  1. spring-projects-issues commented on Feb 28, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    Thanks for the repro project, Leigh. We'll take a look.

  2. spring-projects-issues commented on Apr 2, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Dridi Boukelmoune commented

    I'm encountering the exact same symptoms as described above.

    My environment:
    RHEL, Tomcat 7.0.25, Hotspot 1.6.0_24 64bits, Spring 3.1.1.RELEASE
    I also use RESTEasy spring integration through Spring MVC

    I'd like to mention that the "leak" ratio for dependentBeanMap and dependenciesForBeanMap in DefaultListableBeanFactory are the same as this issue:
    https://issues.springsource.org/browse/SPR-7764

    My heap dump shows 500m/101m vs 265m/49m in issue 7764

  3. spring-projects-issues commented on Apr 4, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Dridi Boukelmoune commented

    I've tested the request-memory-leak webapp, and as a first workaround, I found that @Autowired won't leak.

  4. spring-projects-issues commented on Apr 4, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Dridi Boukelmoune commented

    I've uploaded a quick fix for the 3.1.x branch that doesn't break the tests.
    The leak is gone but I'm not sure the piece of code I've written is correct, though I read a very lot of your code trying to spot differences between @Resource and @Autowired.

  5. spring-projects-issues commented on May 1, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    Thanks again, Leigh for the repro project - I've just taken a look, and indeed it demonstrates the issue quite clearly.

  6. spring-projects-issues commented on May 1, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    Also note that I've added the repro project at

    spring-attic/spring-framework-issues@e6285b5

    You can read about how to do this yourself for future issues at https://github.com/SpringSource/spring-framework-issues#readme

  7. spring-projects-issues commented on May 1, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    commit ae9549ae1388c8b18872b84eadc9e3068b4e1d25
    Merge: e85e614 f779c19
    Author: Chris Beams <[email protected]>
    Date:   Tue May 1 17:52:21 2012 +0300
    
        Merge pull request #66 from dridi/SPR-9176
        
        * SPR-9176:
          Fix scoped-proxy memory leak w/ @Resource injection
    
    commit f779c199ea272cf61781e120b1ab2efc50de0cbb
    Author: Dridi Boukelmoune <[email protected]>
    Date:   Mon Apr 16 20:17:39 2012 +0300
    
        Fix scoped-proxy memory leak w/ @Resource injection
        
        Prior to this change, request-scoped components having
        @Resource-injected dependencies caused a memory leak in
        DefaultListableBeanFactory#dependenciesForBeanMap.
        
        Consider the following example:
        
            @Component
            @Scope(value="request", proxyMode=ScopedProxyMode.TARGET_CLASS)
            public class MyComponent {
        
                @Resource
                private HttpServletRequest request;
        
                // ...
            }
        
        The bean name for "MyComponent" will end up being
        'scopedTarget.myComponent', which will become a key in
        the #dependenciesForBeanMap structure.
        
        On the first request, the injected HttpServletRequest bean will be a
        proxy and will internally have a bean name of the form
        "$Proxy10@1a3a2a52". This name will be added to the Set value associated
        with the 'scopedTarget.myComponent' entry in #dependenciesForBeanMap.
        
        On the second request, the process will repeat, but the injected
        HttpServletRequest will be a different proxy instance, thus having a
        different identity hex string, e.g. "$Proxy10@5eba06ff". This name will
        also be added to the Set value associated with the
        'scopedTarget.myComponent' entry in #dependenciesForBeanMap, and this
        is the source of the leak: a new entry is added to the set on each
        request but should be added only once.
        
        This commit fixes the leak by introducing caching to
        CommonAnnotationBeanPostProcessor#ResourceElement similar to that already
        present in AutowiredAnnotationBeanPostProcessor#AutowiredFieldElement
        and #AutowiredMethodElement. Essentially, each ResourceElement instance
        now tracks whether it has been created, caches the ultimate value to be
        injected and returns it eagerly if necessary. Besides solving the memory
        leak, this has the side effect of avoiding unnecessary proxy creation.
        
        This fix also explains clearly why injection into request-scoped
        components using @Autowired never suffered this memory leak: because the
        correct caching was already in place. Because @Resource is considerably
        less-frequently used than @Autowired, and given that this particular
        injection arrangement is relatively infrequent, it becomes
        understandable how this bug has been present without being reported
        since the introduction of @Resource support in Spring 2.5: developers
        were unlikely to encounter it in the first place; and if they did, the
        leak was minor enough (adding strings to a Set), that it could
        potentially go unnoticed indefinitely depending on request volumes and
        available memory.
        
        Issue: SPR-9176
    
  8. added
    type: bug
    in: web
    on Jan 11, 2019
  9. added this to the 3.1.2 milestone on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: webtype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions