Search code examples
javacglib

cglib - MethodInterceptor & finalize?


I use cglib MethodInterceptor to wrap a service. In each call to the service it's supposed to open data session, pass call to the service and finally close the session.

However, I noticed it misbehaves when invoked from Finalizer. I get the following stacktrace:

java.lang.IllegalArgumentException: interface my.pkg.SomeInterface is not visible from class loader
        at java.lang.reflect.Proxy.getProxyClass(Proxy.java:353)
        at java.lang.reflect.Proxy.newProxyInstance(Proxy.java:581)
        at my.pkg.ProxyFactory.create(ProxyFactory.java:68)
        at my.pkg.SomeService.make(SomeService.java:181)
        at my.pkg.SomeService$SessionWrappingInterceptor.intercept(SomeService.java:1275)
        at my.pkg.SomeService$$EnhancerByCGLIB$$b58faf6a.finalize(<generated>)
        at java.lang.ref.Finalizer.invokeFinalizeMethod(Native Method)
        at java.lang.ref.Finalizer.runFinalizer(Finalizer.java:83)
        at java.lang.ref.Finalizer.access$100(Finalizer.java:14)
        at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:160)

What am I doing wrong? How can I resolve it?


Solution

  • When the finalizer finally picks your object (or your CGLIB proxy object) to finalize, the garbage collector has determined that your object is unreachable and is about to be discarded/collected. Let's assume that this is not the only object being collected, and in fact other objects that it might have used in the past (including their classloaders) have since also been collected.

    There's not enough information in your question to be certain about what is going on, but the general guess I have is that the work your ProxyFactory is doing requires classes that USED to be accessible to this classloader but no longer are, possibly due to the fact that you are in the last stages of garbage collection.

    I've learned the hard way that proxies that handle finalize() calls are downright dangerous. In most cases your proxy target really doesn't need to handle that call, but if it does, don't do anything in your proxy handler that is going to create, initialize or otherwise create references to the proxy target. (For instance, my case was a load-on-demand object. When finalize() was called, if the object had not been previously loaded, it would load it and cache the value somewhere that created a new strong reference chain, thereby disallowing the proxy class, its classloader, and many other classes it referenced to be collected. Massive memory leak.)

    My advice (late tho it is) is to disallow your proxy from handling finalize(). CGLIB's Enhancer can be given CallbackFilters to indicate not to do anything with the finalize() method, or if you're using the simple MethodInterceptor, you can check for that yourself.

    One last comment: careful with the CallbackFilters. They can also cause you memory leaks especially if they are coming from a different classloader than CGLIB is coming from! You're going to end up with CGLIB-generated objects that hold on to your CallbackFilter instances that will not be garbage collected.