How do you crash a JVM?

前端 未结 27 1137
故里飘歌
故里飘歌 2020-11-28 00:22

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

相关标签:
27条回答
  • 2020-11-28 01:05

    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).

    0 讨论(0)
  • 2020-11-28 01:05

    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.

    0 讨论(0)
  • 2020-11-28 01:07

    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.

    0 讨论(0)
  • 2020-11-28 01:10

    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".

    0 讨论(0)
  • 2020-11-28 01:11

    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

    0 讨论(0)
  • 2020-11-28 01:11

    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.

    0 讨论(0)
提交回复
热议问题