views:

602

answers:

5

Recently I converted a Swing application to Webstart. The process was pretty straightforward, but I found that after I close all windows, my application's JVM did not terminate. The thread dump showed that there are a couple of non-daemon threads, notably Swing's EDT, AWT and a couple of websart related threads.

The actual strategy used is that each window increments a counter when it is created and decrements one when it is closed. The default close operation is DISPOSE_ON_CLOSE. Wen the counter reaches zero, I stop all threadpools and release all JNI resources.

When I launched the application from a bat file (same JARs), it terminated fine when all windows were closed, so I figured that the problem has something to do with Webstart.

Now the questions:

  1. Can anybody tell me what exactly is happening? Why does Webstart leave zombie JVMs?
  2. Is there a way to release the Webstart resources explicitly without halting the JVM?
  3. I've always had the opinion that calling System.exit() encourages the sloppy practice of not releasing your resources and relying on the OS to clean up after you (which can lead to nasty surprises if you reuse the code later)... am I missing something?

See also the followup question for detecting whether the app has been launched by Webstart.

A: 

Webstart starts the Console window (you may be able to disable that). The console window is used to see stdout/err of the webstart process as well as rudimentary log/debug but has the side effect of created a top-level AWT/Swing window. Since the AWT/EDT only ends when the LAST window is disposed, the console window is holding up your application. You should probably call System.exit() to be 100% sure your application exits (unless you can gurantee a certain client configuration, webstart console turned off)

basszero
nope. the default configuration is no console window. no windows on the screen whatsoever (empty taskbar, alt+esc does not do anything)
ddimitrov
+1  A: 

The AWT EDT is usually the culprit. For some years now it has had some logic to shutdown when there are no undisposed windows. However, there are recurrent problems with leaks, including within the implementation of AWT and Swing. Therefore, I strongly suggest using System.exit in production releases (you might possibly want to leave it out for some testing to detect leaks).

The WebStart thread should all be daemon when there are no system windows (console, javax.jnlp services and other dialogs) showing.

Tom Hawtin - tackline
Can you give an example for a leak within Swing (i.e. allocating resource which can not be released using the public API)?
ddimitrov
+4  A: 

Because of bugs in WebStart, yes. WebStart starts up a "secure thread" for it's own purposes that interacts with the EDT. This SecureThread prevents the automatic termination of the Java process one would expect when all windows and AWT resources are disposed.

For more information see http://www.pushing-pixels.org/?p=232

shemnon
Thanks, the link explains everything I wanted to know. Is there any bug parade ticket that I can vote on? (if yes, please post the reply as a comment to the main question)
ddimitrov
A: 

Consider attaching with jconsole and get a look at what the JVM is doing.

Thorbjørn Ravn Andersen
A: 

I experience the same issue with web start. If i turn off java console, the process does not hang up. Any known bug id from Sun?