What can cause Java to keep running after System.exit()?

uckelman picture uckelman · Apr 10, 2010 · Viewed 31.7k times · Source

I have a Java program which is being started via ProcessBuilder from another Java program. System.exit(0) is called from the child program, but for some of our users (on Windows) the java.exe process associated with the child doesn't terminate. The child program has no shutdown hooks, nor does it have a SecurityManager which might stop System.exit() from terminating the VM. I can't reproduce the problem myself on Linux or Windows Vista. So far, the only reports of the problem come from two Windows XP users and one Vista user, using two different JREs (1.6.0_15 and 1.6.0_18), but they're able to reproduce the problem every time.

Can anyone suggest reasons why the JVM would fail to terminate after System.exit(), and then only on some machines?

Edit 1: I got the user to install the JDK so we could get a thread dump from the offending VM. What the user told me is that the VM process disappears from VisualVM as soon as he clicks on the 'Quit' item in my menu---but, according to Windows Task Manager, the process hasn't terminated, and no matter how long the user waits (minutes, hours), it never terminates.

Edit 2: I have confirmed now that Process.waitFor() in the parent program never returns for at least one of the users having the problem. So, to summarize: The child VM seems to be dead (VisualVM doesn't even see it) but the parent still sees the process as live and so does Windows.

Answer

Molten Ice picture Molten Ice · May 10, 2017

This can happen if your code (or a library you use) has a shutdown hook or a finalizer that doesn't finish cleanly.

A more vigorous (so should only be used in extreme cases!) way to force shutdown is by running:

Runtime.getRuntime().halt(0);