On May 19, 2013, at 10:01 AM, Caldarale, Charles R wrote: >> From: Nick Williams [mailto:nicho...@nicholaswilliams.net] >> Subject: Re: LOG4J2-223: IllegalStateException thrown during Tomcat shutdown >> (memory leak, it looks like) > >> Log4j 1 never required a listener to be configured to be shut down >> properly when an application is undeployed. > > What bearing does that have on a different logging mechanism?
To be fair, Log4j 2 is not a different logging mechanism. It is a new version of Log4j 1. My point was mostly philosophical; it "feels wrong" to have to configure a listener just to support logging. > >> It should be possible to do this without a listener. > > Not easily. > >> Could a `finalize` method be used instead of a shutdown hook/listener? > > Finalizers should be avoided like the plague. The gyrations the JVM has to > go through to handle them result in continual run time impacts, and require > at least two GC passes to actually get rid of the objects. The extra performance impact is bad, yes, when you're talking about an object who has many short-lived instances in memory that could be garbage collected regularly. However, when you're talking about a lone singleton instance that is created when the application starts and garbage collected when the application shuts down, I would argue this is not a problem at all. Of course, I'm open to the idea that I could be proven wrong. > >> What I don't know is if it is guaranteed to be called in non-web >> applications >> when the JVM just shuts down. > > Finalizers are not called at JVM termination, since the process exit is > expected to release resources automatically. You cannot actually count on a > finalizer ever being invoked; it's one of those "seemed like a good idea at > the time" things that is now widely regretted by JVM implementers. After some experimentation, it would appear that it's not so much that finalizers are not called at JVM termination as it is that finalizers are not called if the garbage collector never runs, and the garbage collector isn't guaranteed to run at JVM shutdown. However, if you call System.gc() right before the JVM shuts down, finalizers appear to run every time. Unfortunately, requiring users to call System.gc() before shutdown for logging to work properly is no better than requiring users to register a listener in a web application for logging to work properly. Surely there's a better way... While I do not disagree that finalizers are very often misused, they are not without their uses. I find it hard to believe that the Sun JRE 6 source code would contain 50 uses of finalizers if they should never be used. You'd think if they regretted creating the method so much they would deprecate it and/or document clearly that it's best to never implement a finalizer, but the documentation for finalizers makes no such assertion, even in the latest Java 8. > > - Chuck > > > THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY > MATERIAL and is thus for use only by the intended recipient. If you received > this in error, please contact the sender and delete the e-mail and its > attachments from all computers. > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org > For additional commands, e-mail: users-h...@tomcat.apache.org > --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org