Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak due to CGLIB 3.2.0 fast classes #1042

Open
kenwenzel opened this issue Sep 13, 2016 · 1 comment
Open

Memory leak due to CGLIB 3.2.0 fast classes #1042

kenwenzel opened this issue Sep 13, 2016 · 1 comment

Comments

@kenwenzel
Copy link
Contributor

I use the method Injector.createChildInjector(...) to create many injectors with different configurations in my application. Each of those injectors is only used for a short period of time and then garbage collected.

Now it seems that for each injector an own set of fast classes is created for each SingleMethodInjector and others. For an example look at:

final net.sf.cglib.reflect.FastClass fastClass =

These fast classes (or at least meta data about them) are kept within a global map.
The Eclipse Memory Analyzer classifies this as a leak suspect:

One instance of "java.util.HashSet" loaded by "<system class loader>" occupies 671,209,552 (79.21%) bytes.
The instance is referenced by com.google.inject.internal.cglib.core.$AbstractClassGenerator$1 @ 0xfff4c7a0 ,
loaded by "com.google.inject". The memory is accumulated in one instance of "java.util.HashMap$Node[]" loaded by "<system class loader>".

I think it should be possible for Guice to share the fast classes between different injectors.

These are my findings after digging into the problem:

My proposed fix:

kenwenzel added a commit to kenwenzel/guice that referenced this issue Sep 30, 2016
* Fixes google#1042
* Creates a sub-class of FastClass.Generator to implement hashCode() and equals().
@kenwenzel
Copy link
Contributor Author

kenwenzel commented Oct 4, 2016

I've looked at the wrong version of CGLIB (the latest).

The problem seems to be the class name cache of CGLIB v3.2.0 (used by Guice):
https://github.com/cglib/cglib/blob/RELEASE_3_2_0/cglib/src/main/java/net/sf/cglib/core/AbstractClassGenerator.java#L233

The generated class is cached via a WeakReference:
https://github.com/cglib/cglib/blob/RELEASE_3_2_0/cglib/src/main/java/net/sf/cglib/core/AbstractClassGenerator.java#L242

If then a Guice injector is gc'ed and a new one is created the fast classes are regenerated and their names are added to the class name cache. Hence the class name cache grows over time ...

Proposed fix: Switch to CGLIB 3.2.4

@kenwenzel kenwenzel changed the title Memory leak and performance problem due to CGLIB fast classes Memory leak due to CGLIB 3.2.0 fast classes Oct 4, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant