Skip to content

Cglib2AopProxy weak reference causes perm space to fill up [SPR-4004] #8684

Closed

Description

@spring-projects-issues

Alex Burgel opened SPR-4004 and commented

after upgrading to 2.0.7 i started getting a bunch of out of memory errors in my perm space. i think it has something to do with cglib proxies.

my method to create proxied objects:

public static Object create(Object obj) {
    Advisor advisor = new DefaultPointcutAdvisor(VERSIONING_POINTCUT, new VersioningAdvice());

    ProxyFactory pf = new ProxyFactory(obj);
    pf.setProxyTargetClass(true);
    pf.setOptimize(true);
    pf.addAdvisor(advisor);
    pf.setFrozen(true);

    return pf.getProxy();
}

obj is a class, so cglib is used to create the proxy, which means it will generate a new subclass that handles the proxying. cglib uses Cglib2AopProxy$ProxyCallbackFilter as part of a key to a map that holds the generated classes so it doesn't have to regenerate a new class each time.

this worked in 2.0.6, but in 2.0.7 ProxyCallbackFilter uses a WeakReference to hold the advised object, which in my case is pf from the method above. once this method is complete, there are no strong reference to pf anymore, so after the next garbage collection the WeakReference will be cleared.

Cglib2AopProxy$ProxyCallbackFilter.equals uses the advised object (line 861), so after a garbage collection, two ProxyCallbackFilters that should have been equal are no longer because one of the advised references has been cleared.

this causes cglib to generate another class. so after every garbage collection, cglib will generate a new class, which will eventually lead to out of memory errors.

i have a feeling that my way of using ProxyFactory is not the best approach, but i'm not sure what other way to achieve this same behavior (other than using aspectj weaving, which i'd rather not do).


Affects: 2.0.7

Issue Links:

0 votes, 6 watchers

Activity

  1. spring-projects-issues commented on Nov 4, 2007

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    This is a side effect of the fix for #8302 which claimed a memory leak through holding a strong reference to the Advised object in ProxyCallbackFilter there... Any suggestions on how we could resolve this, satisfying both requirements?

    Juergen

  2. spring-projects-issues commented on Nov 5, 2007

    @spring-projects-issues
    CollaboratorAuthor

    Alex Burgel commented

    i think the problem is that the ProxyCallbackFilter only needs access to the advisors and the class of the target source, not the actual target source itself. but since it maintains a reference to the target source, you get the memory leak in #8302 because cglib caches the ProxyCallbackFilter.

    so if there is some way to have the ProxyCallbackFilter not keep a reference to the target source but without using a weak reference, then both problems should be solved.

    i think the options are 1) to detach the target source (but keep a reference to its class) from the original AdvisedSupport, 2) clone the AdvisedSupport, but use a special TargetSource that only keeps a reference to the class of the object

    neither option seems all that satisfying though.

  3. spring-projects-issues commented on Nov 9, 2007

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    I've fixed this through cloning the AdvisedSupport into a configuration-only copy, replacing the active TargetSource with an EmptyTargetSource that indicates the same target class (and same static/dynamic flag). This configuration-only copy is now held in a strong reference again, which shouldn't cause a problem with garbage collection anymore.

    Juergen

  4. spring-projects-issues commented on Nov 9, 2007

    @spring-projects-issues
    CollaboratorAuthor

    Alex Burgel commented

    this fix works for me (using snapshot spring-framework-2.0.8-20071109-128)

    thank you!

  5. added
    type: bug
    in: core
    on Jan 11, 2019
  6. added this to the 2.0.8 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

Labels

in: coretype: bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions