Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

java.io.NotSerializableException: java.util.logging.Logger

Peter Olah
1 decade ago Jan 13, 2012 1:41pm
Deleted User
1 decade ago Jan 13, 2012 6:36pm
Henri Sara
1 decade ago Jan 14, 2012 12:26pm
Bobby Bissett
1 decade ago Jan 15, 2012 5:49pm

Jeyaraj G: i think you need to declare the Logger as static final transient.

private static final transient Logger logger = Logger.getLogger(yourClass);

I'd like to point out that you should never declare a logger static in a web application or any other application that's loaded by a container. Generally speaking, never hold a static reference to an object that is also referenced by a class loader outside of your application. In the logger case, the Logger class in the JVM holds a map of strings (logger name) to weak references to logger objects.

Whenever you redeploy your app, the previous classloader for the app holds a reference to the logger object (since it's static), and so does the Logger class in the JVM. So the classloader never gets garbage collected because the logger is never GC'ed, and you eventually run out of memory after a lot of redeploys. You can find a few articles about this online by searching "java static logger classloader memory" or something. I suppose you could get around this by holding a static weak reference to a logger, but there's really no need. Just declare the logger when you need it at the top of a method, or whenever it's used. If you want it at the class level, just don't make it static, and mark it as transient in a serializable class (no need to ever serialize a class that you can just fetch again from the JVM).

The Logger.getLogger("foo") call is just a map lookup, so it's not that expensive. Even less expensive is having each instance of a class have its own reference to the same Logger. After all, it's the same logger. As a test, guess what this prints:

Logger l0 = Logger.getLogger("example");
        Logger l1 = Logger.getLogger("example");
        System.out.println(l0 == l1);

I know it's one of those things that "everybody does," but in this case "everybody" is actually wrong. :) Someone on the JDK team told me a couple years back that this would be fixed in JDK 7, but I don't honestly know if it is or not.


Last updated on Jan, 15th 2012
David Wall
1 decade ago Jun 10, 2012 8:31pm
Henri Sara
1 decade ago Jun 11, 2012 8:32am
Bobby Bissett
1 decade ago Jun 20, 2012 6:36pm
Sebastian Paul
1 decade ago Aug 01, 2012 3:23pm