Uploaded image for project: 'Hibernate ORM'
  1. Hibernate ORM
  2. HHH-2481

Big memory leak in the use of CGLIB

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Critical
    • Resolution: Fixed
    • Affects Version/s: 3.2.2
    • Fix Version/s: 3.2.3
    • Component/s: hibernate-core
    • Labels:
      None
    • Environment:
      Hibernate 3.2.2
      MySQL 5.1
    • Bug Testcase Reminder (view):

      Bug reports should generally be accompanied by a test case!

    • Last commented by a user?:
      true

      Description

      The way CGLIBLazyInitializer is creating proxies is resulting in a potentially massive memory leak.

      In CGLIBLazyInitializer.getProxy() just before the proxy is instantiated, a call is made to Enhancer.registerCallbacks() passing in the instance of CGLIBLazyInitializer that will manage the proxy. This variable is stored in a static ThreadLocal on the CGLIB created persistentClass so that any subsequent objects instantiated will get this callback class.

      The problem is that once this ThreadLocal is set, it is never cleared, so it will stay around (together with the object it's managing, and whatever object graph it may be connected to) until the next time a proxy is created for that type on that thread.

      For our application we have about 150 different proxy types, and our app can have over 100 threads. This results in potentially 15,000 proxy objects and their graphs stuck in memory.

      The fix for this is simple. Just make another call the Enhancer.registerCallbacks() with a null callback arg, right after the proxy class is instantiated:

      Enhancer.registerCallbacks(factory, null);

        Attachments

          Issue links

            Activity

              People

              • Votes:
                3 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Time Tracking

                  Estimated:
                  Original Estimate - 5m
                  5m
                  Remaining:
                  Remaining Estimate - 5m
                  5m
                  Logged:
                  Time Spent - Not Specified
                  Not Specified