Skip to content

Support for ByteBuddy as an alternative to CGLIB [SPR-8190] #12840

Closed as not planned

Description

@spring-projects-issues

Chris Beams opened SPR-8190 and commented

Tracking issue for CGLIB problems and possible solutions when moving to Javassist or, as recently suggested, Byte Buddy.


Attachments:

Issue Links:

44 votes, 45 watchers

Activity

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

    @spring-projects-issues
    CollaboratorAuthor

    Kyrill Alyoshin commented

    Is it possible to give this issue a bit for weight. CGLIB is one big disaster. It comes with an old ASM version, unmaintained, and buggy. I think Spring is the only project left that is actually using it.

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

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    @Kyrill,

    It's definitely on the radar. It would be helpful to hear about exactly what pain this causes you... is it actually ASM version conflicts, for example? What else?

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

    @spring-projects-issues
    CollaboratorAuthor

    Kyrill Alyoshin commented

    ASM is huge. I have no firm evidence for it, but I also think CGLIB still leaks memory in some scenarios. Fundamentally, CGLIB prevents us from using what you, Chris, have been working so hard on: configuring Spring in Java. :-)

  4. spring-projects-issues commented on Jul 10, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Jason Day commented

    I have issues with CGLIB leaking memory in OSGi environments where a bundle that uses CGLIB to create proxies will not let them go, even after removing the bundle. I suspect it's related to the root of #8831. The result is that the permgen space eventually runs out after multiple re-deploys of bundles that use CGLIB. Currently, it's merely an inconvenience in our development environments that requires us to occasionally restart our instances since we plan to restart production instances on every application deployment.

    To reproduce the issue, create a Spring bundle with a CGLIB enhanced, for example an empty @Configuration file such as the following:

    @Configuration
    public class MyConfig {
      // class is enhanced by CGLIB
    }

    Then deploy the bundle to an OSGi environment. Each time the bundle is stopped and started again a new instance of the MyConfig class is re-created, each time enhanced by CGLIB, which gets cached. Take a thread dump and search for "MyConfig" in the heap and you'll see each CGLIB enhanced instance hanging around. This was produced with Spring 3.1.1 and CGLIB 2.2.

  5. spring-projects-issues commented on Dec 19, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Phil Zoio commented

    Get a similar issue when using module redeploys using Impala. Any module which uses the @Transactional where the proxying is class rather than interfaces based is affected. Each time module reloading recurrs for these modules, some more perm gen space is used up, until it eventually runs out.
    Ultimately, this issue strongly discourages making any hot redeploy of modules available in production environments, so it would be a real bonus to get it sorted.

    Have been using Spring 3.0.6 and Cglib 2.2. Is this problem likely to have been fixed by an upgrade to Spring 3.2, and if not, how much longer should we expect for a resolution?

  6. spring-projects-issues commented on Dec 19, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    Phil, I'd like to make sure this is pinned down once and for all. Can you provide me with reproduction steps to get up and running with the simplest possible impala-based app that demonstrates this issue? While you're at it, please double check against 3.2. We did make some tweaks to how CGLIB caching is done there, but I cannot be sure whether it will solve your problem without looking more closely.

  7. spring-projects-issues commented on May 16, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Tim Jones commented

    I am not sure how far this issue has progressed but if it helps I have created a few reproducible tests that can be run in Karaf that I think highlight the issues discussed here and in https://jira.springsource.org/browse/SPR-5654

    Attached is a zip file that contains the tests and hopefully reasonable instructions on how to set up the tests. I have also added a few observations of my own.

    In summary I have set up 2 bundles, with one bundle exporting a transaction manager and the other bundle referencing the transaction manager with a class that has been transactionally advised via a @Transactional annotation.

    The tests are further divided with one group dependent on Spring 3.1.4 and the second group dependent on Spring 3.2.2. Within these two groups there are two referencing bundles, one that is coded against interfaces (=> JDK proxy) and the other without interfaces (=> CGLIB).

    Both the Spring 3.1.4 and Spring 3.2.2 test bundles that use interfaces seem to be fine, i.e. the referencing bundle can be loaded and unloaded many times without resulting in a OutOfMemoryError: PermGen space error. I was able to verify using JVisualVM that the classes are collected although there seems to be a difference as to when they are collected (details of which are in the readme of the zip).

    Both the Spring 3.1.4 and Spring 3.2.2 test bundles that do not use interfaces and hence CGLIB comes into play ultimately fail with a OutOfMemoryError: PermGen space error. I was able to verify using JVisualVM that the classes are not collected and that after uninstalling the referencing bundle that instances of the test classes still remain.

    It is possible that the tests could be simplified further, perhaps one bundle rather than two would suffice but I think separating the transaction manager is more realistic.

    I realise that class loader permgen leaks are an old well trodden issue that came to the forefront when undeploying web apps but because OSGI permits breaking an application down into fine grained modules I think this issue will become even more apparent as the promise of undeploying/stopping/starting parts of an application becomes a significant driver for the adoption of OSGI.

    I hope this helps.

    Regards,

    Tim

  8. spring-projects-issues commented on May 16, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Tim Jones commented

    Tests highlighting perm gen leaks using Karaf

  9. spring-projects-issues commented on May 17, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    Tim, thank you for the detailed report. An actual move from CGLIB to Javassist is not high on our priority list right now. Could you create a new Bug, copy your comment over as the description and attach the zip there? That is more likely to get attention in the near term.

  10. spring-projects-issues commented on May 19, 2013

    @spring-projects-issues
    CollaboratorAuthor
  11. spring-projects-issues commented on May 6, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Rafael Winterhalter commented

    Did you ever consider moving to something else then javassist? I want to suggest Byte Buddy (http://bytebuddy.net) as an option which is a rather new library. For full disclosure, note that I am the author of this library but I still hope this posting is not conceived as too self-promotional. I wrote the library after working with cglib and javassist for quite a while and I believe that I took the best ideas out of both frameworks but replaced the bad ideas with something better. I think Spring could profit especially by Byte Buddy's retention of primitive types, the smaller-sized proxy classes and the improved support for implementing around-advice. At last, with Byte Buddy repackaging ASM correctly from day one, users would not have to deal with cglib version conflicts anymore.

    Finally, note that the makers of cglib moved their project to GitHub where they however stated that the library is officially not longer under active development. (cglib/cglib#3)

  12. spring-projects-issues commented on Sep 22, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    We intend to stick with CGLIB for the time being, picking up CGLIB 3.2 whenever it gets released (which still seems to be planned).

    Juergen

  13. spring-projects-issues commented on Oct 12, 2016

    @spring-projects-issues
    CollaboratorAuthor

    cemo koc commented

    Spock also has just started use ByteBuddy. Mockito is using as well. It might be a good idea to revisit ByteBuddy to replace CGLIB for Spring 5.

  14. spring-projects-issues commented on Oct 12, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    From my perspective, we're happy enough with our repackaged version of CGLIB 3.2.4 against our custom ASM fork at this point, so this isn't a high priority for us in 5.0. I'm sure ByteBuddy is a fine piece but I'm also sure we're going to have some trouble and some regressions, beyond the migration effort to begin with. Keep in mind that we are not just using CGLIB for AOP proxies but also for runtime-generated configuration subclasses and generated lookup methods, for example.

    In any case, I've brought this ticket back to life, leaving it in the 5.x backlog for the time being.

  15. spring-projects-issues commented on Oct 12, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Rafael Winterhalter commented

    I am currently helping Hibernate to migrate from Javassist to Byte Buddy after they had some issues and Spring is a big enough framework that I can offer you to try building a prototype.

    Since Byte Buddy is just a class generator, it should be easy enough to switch library. In the end, only the generated code is exposed. If you wanted to, you could even mimic the "PoweredByCglib" part of the class name even though I would suggest something more meaningful such as SpringProxy or the like. Let me know if you are interested in my help, porting something from the cglib Enhancer is usually very straight forward!

    Also, FYI, I still use some time maintaining cglib but the development has ended. We are only integrating critical bug fixes but we do not plan to apply any bigger extensions for future Java releases.

  16. 12 remaining items

  17. spring-projects-issues commented on Feb 13, 2017

    @spring-projects-issues
    CollaboratorAuthor

    Rafael Winterhalter commented

    I understand the concern and it is true that I have changed some APIs in Byte Buddy before the library became so popular. With users like Hibernate and Mockito I am more cautious with changing APIs. Sometimes, it is unfortunately not avoidable as Java byte code - unless Java - is not always backwards compatible itself but I think the APIs are isolated appropriately.

    As the user with the problems referred to breaking changes in the recent versions, I can only assume that the mentioned internal library relied on Byte Buddy internal APIs or very bleeding-edge features like module redefinition which some people use to make their software run on Java 9 where the APIs change simply because Java 9 is not yet final.

    I do not think this should be a big problem for an optional binding like that one planned for Spring.

  18. spring-projects-issues commented on Aug 9, 2018

    @spring-projects-issues
    CollaboratorAuthor

    Rafael Winterhalter commented

    Just a quick follow-up: if it was possible to refactor the proxy supplier in general, one could consider to generate proxy classes during build time. Byte Buddy's proxy classes are stateless as they are currently implemented such that the runtime could simply locate the classes by their name. These names could for example be added as an annotation value to the proxied classes as a part of the same build operation.

    This had the obvious advantage of avoiding class generation during runtime what is expensive. Furthermore, the Byte Buddy dependency would no longer be required during runtime what reduces the footprint of the using application. Finally, this would allow the usage of the proxy mechanism with full AOP capabilities even for native-image generation using Graal or other AOT compilation.

    If this was a desired feature, I would happily provide some help creating this implementation. The dynamic proxy generation would however still be helpful, especially during testing where on-demand generation still makes perfect sense.

    I created a small POC for this idea: https://github.com/raphw/proxy-example

  19. added
    type: enhancement
    in: core
    has: votes-jira
    on Jan 11, 2019
  20. ma-sharifi commented on May 8, 2023

    @ma-sharifi

    Is there any update on this?

  21. jhoeller commented on Jul 12, 2023

    @jhoeller
    Contributor

    For the time being, we are going to hang on to our CGLIB fork in the 6.x generation. It does everything we need it do for AOT purposes and for JDK 17+ compatibility, with a pretty lean codebase that we can patch wherever and whenever we need. Supporting a different bytecode generation strategy is a huge maintenance burden that we're currently not ready to take on.

  22. Sineaggi commented on Jul 13, 2023

    @Sineaggi
    Contributor

    @jhoeller perhaps the Class-File API JEP might be interesting when it stabilizes?

  23. removed this from the General Backlog milestone on Jul 13, 2023
  24. added
    status: declined
    on Jul 13, 2023
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

    has: votes-jirain: corestatus: declinedtype: enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions