tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dan Armbrust <daniel.armbrust.l...@gmail.com>
Subject Re: How to handle the AWT-Windows thread?
Date Thu, 18 Aug 2011 21:22:50 GMT
On Thu, Aug 18, 2011 at 2:13 PM, Christopher Schultz
<chris@christopherschultz.net> wrote:

> The JVM should not launch more than one AWT thread, so you should be
> okay. The only issue would be whether or not it inherits the webapp's
> context ClassLoader which would really represent a memory leak if you
> do live webapp reloads (or undeploy/redeploy).
>
> You would only get this message if the WebappClassLoader was being
> used as the ContextClassLoader for this thread, so that must be the case.
>
> You should be able to trick the AWT thread to start using the system
> class loader in a ServletContextListener that looks something like this:
>
> init() {
>
>  Thread myThread = Thread.currentThread();
>  ClassLoader ccl = myThread.getContextClassLoader(); // PUSH
>  myThread.setContextClassLoader(ClassLoader.getSystemClassLoader());
>
>  // do something that triggers the creation of the AWT -- maybe
>  // something like "new java.awt.Frame()"
>
>  myThread.setContextClassLoader(ccl); // POP
> }
>
> This will pin the system ClassLoader into memory (big deal) instead of
> your webapp's. This should fix this particular leak and, by extension,
> remove the error message from your logs.
>
> - -chris

You sir, are a clever clever man :)

Works perfectly.  The AWT thing that I did was simply

Toolkit.getDefaultToolkit().createImage(new byte[]{});

which avoids "Headless" issues, but still kicks off the AWT-Windows thread.

And since it is no longer tied to the context class loader, Tomcat
doesn't detect any issues on shutdown.

Thanks,

Dan

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message