简体   繁体   中英

Memory leak in sun.print.Win32PrintServiceLookup.notifyPrinterChange

When I stop my Tomcat server in windows sometimes I have to wait long time, in these cases I got this warning:

org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [ROOT] appears to have started a thread named [Thread-6] but has failed to stop it. This is very likely to create a memory leak.
Stack trace of thread:
 sun.print.Win32PrintServiceLookup.notifyPrinterChange
 sun.print.Win32PrintServiceLookup.access$100(Unknown Source)
 sun.print.Win32PrintServiceLookup$PrinterChangeListener.run(Unknown Source)

The only printer-connected code in my web app is a reader of available printers with:

PrintService[] printServices = PrinterJob.lookupPrintServices();

Do you know how to avoid this problem?

That thread is started by Win32PrintServiceLookup itself , unless it thinks you are on Windows 98 , which it verifies by using system property os.name , so here is a funky (tentative) workaround:

String osName = System.getProperty("os.name");
if (osName.startsWith("Windows")) { // or SystemUtils.IS_OS_WINDOWS if you use commons-lang
    System.setProperty("os.name", "Windows 98");  // Pretend you're on Win98
}
PrinterJob.lookupPrintServices();
System.setProperty("os.name", osName); // Restore original value

I have not tested (no Windows system around today), so I just hope it still gets the list once. It also means you lose the ability to register changes of printers during the application lifetime.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM