I was reading a book on programming skills wherein the author asks the interviewee, \"How do you crash a JVM?\" I thought that you could do so by writing an infinite for-loo
Shortest? Use Robot class to trigger CTRL+BREAK. I spotted this when I was trying to close my program without closing console (It had no 'exit' functionality).
Does this count ?
long pid = ProcessHandle.current().pid();
try { Runtime.getRuntime().exec("kill -9 "+pid); } catch (Exception e) {}
It only works for Linux and from Java 9.
For some reason I don't get, ProcessHandle.current().destroyForcibly();
doesn't kill the JVM and throws java.lang.IllegalStateException
with the message destroy of current process not allowed.
If you want to crash JVM - use the following in Sun JDK 1.6_23 or below:
Double.parseDouble("2.2250738585072012e-308");
This is due to a bug in Sun JDK - also found in OpenJDK. This is fixed from Oracle JDK 1.6_24 onwards.
Not a crash, but closer to a crash than the accepted answer of using System.exit
You can halt the JVM by calling
Runtime.getRuntime().halt( status )
According to the docs :-
"this method does not cause shutdown hooks to be started and does not run uninvoked finalizers if finalization-on-exit has been enabled".
here is a detailed explanation on what causes JVM to core dump (i.e. crash): http://kb.adobe.com/selfservice/viewContent.do?externalId=tn_17534
I'm doing it now, but not entirely sure how... :-) JVM (and my app) sometimes just completely disappear. No errors thrown, nothing logged. Goes from working to not running at all instantly with no warning.