Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is finalize called on singletons when a classloader is released?

By "released" I mean there are no references to the classloader remaining.

We're running into a problem where a frequently-redeployed Java EE application eats up permgen space. Analysis shows that a singleton in the Java EE app has passed references to application-classloader objects outside of the application (in violation of Java EE rules) and isn't clearing them when the app is undeployed.

Assuming there are no other references remaining to the singleton or the class object, will the singleton's finalize() be called when its class's classloader is released? I'd like to clear the rogue in-bound references there. Or am I in a catch-22 where finalize won't get called until the classloader itself can be garbage-collected - and thus will never be called because of the rogue external references?

The main question here is perhaps:

Will a class object be garbage-collected in this case where its classloader can't be yet? This may be dependent on the specification of classloader behavior, or may be implementation-dependent.

References (the other kind! ;-)) would be appreciated, but aren't necessary.

like image 361
Ed Staub Avatar asked Feb 28 '12 21:02

Ed Staub


People also ask

Is finalize guaranteed to be called?

finalize method is not guaranteed. This method is called when the object becomes eligible for GC. There are many situations where the objects may not be garbage collected.

How many times Finalize method is called?

It is invoked only once during the execution of a program. Following are some notable points about the finalize method. Since this method belongs the Object class, which is the super class of all the classes in java, you can override it from any class.

What is finalize in Java?

Finalization is a feature of the Java programming language that allows you to perform postmortem cleanup on objects that the garbage collector has found to be unreachable. It is typically used to reclaim native resources associated with an object.

What happens if Finalize method throws an exception?

If an uncaught exception is thrown during the finalization, the exception is ignored and finalization of that object terminates. So, in this case the "GC will halt the process for that object" and in which case it may be that some its resources are not have been correctly released.


1 Answers

A class with a static reference to it, will is eligible for garbage collection only, if the class loader is eligible for GCing and there are no other references to it.

A class or interface may be unloaded if and only if its defining class loader may be reclaimed by the garbage collector.

http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.7

Furthermore every class has a reference to its classloader. So a class loader is not eligible for GCing as long as there are references to classes loaded by it or objects of those classes from a non collectible objects.

The finalizer is run some time after an object becomes eligible for garbage collection and before the actual GC takes place.

The approach to free incoming references, that prevent GCing, in a finalizer does not work. The finalizer will not be called as long as such references exist because they prevent the object from becoming eligible for garbage collection. For example you cannot break this reference chain from the inside:

singleton instance <--- singleton class <--- class loader <--
<-- any class loaded by that class loader  <-- any object of such a class
<-- object loaded by another classloader referencing such an object or class
like image 102
Hendrik Brummermann Avatar answered Oct 11 '22 12:10

Hendrik Brummermann