Potential memory leak: many child injectors and cglib fast classes

80 views
Skip to first unread message

Ken Wenzel

unread,
Sep 9, 2016, 8:05:10 AM9/9/16
to google-guice
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:
https://github.com/google/guice/blob/f905d59ac9c3c1ad57c755a1abb29c4fb2d6b7fa/core/src/com/google/inject/internal/SingleMethodInjector.java#L46

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.

Any help is appreciated.

Thank you and best regards,
Ken

Ken Wenzel

unread,
Sep 10, 2016, 4:56:42 AM9/10/16
to google-guice
I digged into the problem and these are my findings:

My proposed fix:

I hope this helps to create a fix for this issue.


Any comments are welcome.


Best regards,

Ken

Reply all
Reply to author
Forward
0 new messages